0

我正在尝试合并两个查询。目的是获得一个记录集,其中包含唯一的客户电子邮件以及他们的姓名和基于 MAX(ID) 的最新列表的标题。

使用 DISTINCT 我可以获得电子邮件和 ID,但显然添加标题会破坏这一点。

这是我到目前为止所拥有的:

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

我在这里发现了一些帖子,表明我需要进行自我加入,但我无法让它返回相同数量的记录,但使用标题,它会返回更多。

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

谁能告诉它有什么问题?

4

4 回答 4

2

像这样的东西?

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 <> ''
于 2012-07-06T10:50:50.453 回答
1
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 ;
于 2012-07-06T11:19:17.753 回答
0

而不是distinct,您可以先排序EV_ContactEmail, EV_CusName, EV_ID desc,然后对所有其他列进行分组

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
于 2012-07-06T10:54:42.670 回答
0

请将 'inner join' 更改为 'left external join' :
SELECT DISTINCT MAX(EV_ID) As EV_ID, EV_ContactEmail, EV_CusName
FROM tblEvents ev
left external 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

于 2012-07-06T11:20:52.530 回答