我正在尝试合并两个查询。目的是获得一个记录集,其中包含唯一的客户电子邮件以及他们的姓名和基于 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
谁能告诉它有什么问题?