1
我有一個查詢,我需要從兩個表中計算'userID'。帶有計數和左連接的SQL查詢
SQL查詢:
SELECT DISTINCT TOP 1000 u.id as userID, u.firstName as userFirstName, u.email as userEmail, u.phone as userPhone,
count(ueo.userID) as opensEmailCounter, count(ush.userID) as opensSmsCounter
FROM dbo.Users u
LEFT JOIN dbo.UserEmailsOpens ueo ON u.id = ueo.userID AND ueo.targetID = 4
LEFT JOIN dbo.UserSmsHistory ush ON u.id = ush.userID AND ush.targetID = 4 AND ush.opened = 1
WHERE u.deleted = 0
AND IsNull(u.firstName, '') != ''
AND IsNull(u.email, '') != ''
AND IsNull(u.phone, '') != ''
GROUP BY u.id, u.firstName, u.email, u.phone
但是,結果是不是我所期待。在我進行第二次左連接之後,它給了我錯誤的數字。這是我在某些情況下的兩倍結果,並顯示相同的計數結果(附加屏幕截圖)。
可以顯示錶dbo.UserEmailsOpens定義是什麼? –
您必須在使用子查詢進行連接之前進行計數,或者使用窗口函數進行計數,該窗口函數使用在表連接之前使其具有唯一性的值進行分區。 – xQbert
我不確定,但它可能正在計算空值。你想讓它做到嗎? –