简体   繁体   English

MS SQL基于具有DISTINCT的MAX(ID)选择多个列

[英]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). 目的是获得一个记录集,该记录集具有唯一的客户电子邮件以及基于MAX(ID)的最新列表的名称和标题。

Using DISTINCT I can get the email and ID but obviously adding the title breaks this. 使用DISTINCT,我可以获取电子邮件和ID,但是显然添加标题可以解决此问题。

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 您可以EV_ContactEmail, EV_CusName, EV_ID desc然后再对所有其他列进行EV_ContactEmail, EV_CusName, EV_ID desc而不是distinct

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 SELECT DISTINCT MAX(EV_ID)作为EV_ID,EV_ContactEmail,EV_CusName
FROM tblEvents ev 从tblEvents ev
left outer join tblCustomers cus ON cus.CUS_ID = ev.EV_CustomerID 左外部联接tblCustomers cus ON cus.CUS_ID = ev.EV_CustomerID
WHERE ( 在哪里(
CUS_IsAdmin = 'y' CUS_IsAdmin ='y'
AND CUS_Live = 'y' AND CUS_Live ='y'
AND EV_Live = 'y' AND EV_Live ='y'
AND EV_EndDate >= '2012/7/5 12:00:00 AM' AND EV_EndDate> ='2012/7/5 12:00:00 AM'
AND EV_ContactEmail <> '' AND EV_ContactEmail <>''
) GROUP BY EV_ContactEmail )GROUP BY EV_ContactEmail

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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