max in complex query

Discussion in 'Databases' started by Jamie18, Sep 7, 2007.

  1. #1
    Hey just wondering if anyone had any ideas for me before i remodel this whole query..

    everything here is working perfectly other than the fact that a group may have more than one parent group.. which would than give multiple rows off for each group i'm interested in.

    the groups table has a last_update column that i would like to use to decide which parent group to use.

    i.e. i want to get the group name of the group most recently updated

    i was hoping i could just throw a max(last_update) somewhere in there but i don't know where or how..

    anyways, here's what i have.. don't mind the coldfusion

      SELECT     DISTINCT    
                        Contacts.Contact_ID As Contact_ID1,
                        Contacts.Group_ID,
                        Contacts.First_Name, 
                        Contacts.Last_Name,
                        Phone.NPA as Province,
                        Phone.Phone_ID,
                        Phone.Phone,
                        Phone.LLC, 
                        Phone.WPS_Kickout_Reason as Reason,
                        Groups1.Name as Supplier_Name,
                        Groups.Name as Group_Name,
                        Groups2.Name as Parent_Name,
                        Contacts.Last_Update as Last_Update
                FROM     Groups INNER JOIN Contacts
                            ON Groups.Group_ID = Contacts.Group_ID
                        INNER JOIN Memberships
                            ON Contacts.Contact_ID = Memberships.Contact_ID
                        LEFT JOIN Phone
                            ON Contacts.Contact_ID = Phone.Contact_ID
                        LEFT JOIN Groups AS Groups1
                            ON Phone.Supplier_ID = Groups1.Group_ID
                        LEFT JOIN Page_Links
                            ON Page_Links.Child_ID = Groups.Group_ID
                        LEFT JOIN Groups AS Groups2
                            ON Groups2.Group_ID = Page_Links.Parent_ID
                WHERE    <cfif Form.Region GT "0" and isdefined("submitReg")>
                            (Phone.NPA IN (#exchangelist#) AND WPS_Kickout_Reason IS NOT NULL)
                        <cfelse>
                            Phone like '#PhoneFilter#%'
                            <cfif getGroups.recordCount>
                                AND Memberships.Group_ID IN (#Groups#)
                            </cfif>
                            <cfif isdefined("padOnly")>
                                AND LLC = 1
                            </cfif>
                            <cfif isdefined("kickonly")>
                                AND WPS_Kickout_Reason is not null
                            </cfif>
                        </cfif>
                ORDER BY 
                        Province,
                        Parent_Name,
                        Group_Name,
                        Last_Name,
                        First_Name,
                        Phone
    Code (markup):
    thanks
     
    Jamie18, Sep 7, 2007 IP