简体   繁体   中英

MS SQL select multiple columns based on the MAX(ID) with DISTINCT

I'm trying to merge two queries. The aim is to get a recordset that has unique customer emails along with their name and the title for their latest listing based on MAX(ID).

Using DISTINCT I can get the email and ID but obviously adding the title breaks this.

This is what I have so far:

SELECT DISTINCT MAX(EV_ID) As EV_ID, EV_ContactEmail, EV_CusName
FROM tblEvents ev
INNER JOIN tblCustomers cus ON cus.CUS_ID = ev.EV_CustomerID
WHERE (
    CUS_IsAdmin = 'y'
    AND CUS_Live = 'y' 
    AND EV_Live = 'y' 
    AND EV_EndDate >= '2012/7/5 12:00:00 AM'
    AND EV_ContactEmail <> ''
    )
GROUP BY EV_ContactEmail

I've found some posts on here that say indicate I need to do a self join but I cannot get it to return the same amount of records but with the title, it returns many more.

SELECT DISTINCT MAX(EV_ID) As EV_ID, ev.EV_Title, EV_ContactEmail, EV_CusName
FROM tblEvents ev
INNER JOIN tblCustomers cus ON cus.CUS_ID = ev.EV_CustomerID
INNER JOIN (
    SELECT EV_Title, MAX(EV_ID) AS MaxID
    FROM tblEvents
    GROUP BY EV_Title
) groupedev ON ev.EV_Title = groupedev.EV_Title AND ev.EV_ID = groupedev.MaxID
WHERE (
    CUS_IsAdmin = 'y' 
    AND CUS_Live = 'y' 
    AND EV_Live = 'y' 
    AND EV_EndDate >= '2012/7/5 12:00:00 AM'
    AND EV_ContactEmail <> ''
)
GROUP BY EV_ContactEmail, ev.EV_Title

Can anyone advise what is wrong with it?

Something like this?

SELECT DISTINCT EV_ID, EV_ContactEmail, EV_CusName, EV_Title
FROM tblEvents ev
    INNER JOIN tblCustomers cus 
        ON cus.CUS_ID = ev.EV_CustomerID
    INNER JOIN (SELECT MAX(EV_ID) AS  MaxID
                FROM tblEvents
                GROUP BY EV_ContactEmail) sub
        ON ev.EV_ID = sub.MaxID
WHERE CUS_IsAdmin = 'y'
    AND CUS_Live = 'y' 
    AND EV_Live = 'y' 
    AND EV_EndDate >= '2012/7/5 12:00:00 AM'
    AND EV_ContactEmail <> ''
SELECT e.EV_ID, e.EV_ContactEmail, e.EV_CusName
FROM
        events AS e
    INNER JOIN
        ( SELECT MAX(EV_ID) As EV_ID
          FROM tblEvents ev
            INNER JOIN tblCustomers cus 
              ON cus.CUS_ID = ev.EV_CustomerID
          WHERE CUS_IsAdmin = 'y'
            AND CUS_Live = 'y' 
            AND EV_Live = 'y' 
            AND EV_EndDate >= '20120705T00:00:00'   --- notice the unambiguous
            AND EV_ContactEmail <> ''               --- datetime format
          GROUP BY EV_ContactEmail
        ) AS tmp
      ON tmp.EV_ID = e.EV_ID ;

Instead of distinct , you can first order by EV_ContactEmail, EV_CusName, EV_ID desc and then group all other columns

SELECT MAX(EV_ID) As EV_ID, EV_ContactEmail, EV_CusName
FROM 
(
   SELECT EV_ID As EV_ID, EV_ContactEmail, EV_CusName 
   FROM tblEvents ev 
   INNER JOIN tblCustomers cus ON cus.CUS_ID = ev.EV_CustomerID 
   WHERE (     
      CUS_IsAdmin = 'y'     
      AND CUS_Live = 'y'      
      AND EV_Live = 'y'      
      AND EV_EndDate >= '2012/7/5 12:00:00 AM'     
      AND EV_ContactEmail <> ''     
   ) 
   ORDER BY EV_ContactEmail, EV_CusName, EV_ID DESC -- you can add title also here before ev_id
)
GROUP BY EV_ContactEmail, EV_CusName  -- if you need title in query, also add title here

Please change 'inner join' to 'left outer join' :
SELECT DISTINCT MAX(EV_ID) As EV_ID, EV_ContactEmail, EV_CusName
FROM tblEvents ev
left outer join tblCustomers cus ON cus.CUS_ID = ev.EV_CustomerID
WHERE (
CUS_IsAdmin = 'y'
AND CUS_Live = 'y'
AND EV_Live = 'y'
AND EV_EndDate >= '2012/7/5 12:00:00 AM'
AND EV_ContactEmail <> ''
) GROUP BY EV_ContactEmail

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM