2012-07-06 336 views
0

我試圖合併兩個查詢。目標是獲得一個記錄集,其中包含唯一的客戶電子郵件以及他們的名稱和基於MAX(ID)的最新列表的標題。MS SQL使用DISTINCT選擇基於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 

任何人都可以告訴它有什麼問題嗎?

+1

可你也提供你的數據庫結構 – Kshitij 2012-07-06 10:41:48

回答

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 ; 
+0

這個查詢完美的作品,你已經救了我從一個糟糕的星期五下午!非常感謝。 – DAC84 2012-07-06 12:06:00

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 <> '' 
+0

大,這看起來不錯,雖然第一個查詢的分組是正確的EV_ContactEmail作爲一個管理員(客戶)可以以他人的名義的。這確實會比唯一EV_ContactEmail的總數少返回24行,有沒有一種測試方法爲什麼? – DAC84 2012-07-06 11:04:25

+0

是的,它適合我。我只想知道爲什麼只有選擇電子郵件的基礎上有一個不同的查詢,爲什麼有24條記錄比預期少。有639個不同的電子郵件,但該查詢返回615個記錄。 – DAC84 2012-07-06 11:09:38

+0

因此,查詢'SELECT DISTINCT EV_ContactEmail FROM tblEvents'返回639條記錄? – 2012-07-06 11:16:57

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 
+0

這不起作用。它只是訂購一個有大量重複記錄的記錄集。 – DAC84 2012-07-06 11:06:13

+0

'ORDER BY'在'GROUP BY'之後# – 2012-07-06 11:12:27

+0

@DeanCarslake我想你錯過了將所有記錄分組的部分。我修改了查詢。這應該很好。 – Kshitij 2012-07-06 11:57:53

0

請更改 '內部連接' 到 '左外連接':
SELECT DISTINCT MAX(EV_ID)作爲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秒'
AND EV_ContactEmail <>「」
) GROUP BY EV_ContactEmail

+0

它必須是內部連接,因爲我需要管理員唯一的記錄,因此IsAdmin ='y'過濾器。 – DAC84 2012-07-06 11:23:44