我試圖過濾出由特定用戶添加的結果,同時保持每一天,因爲此查詢將用於報表中的圖表。當我刪除t2.createdBy <> 21
時,我得到了所有的日期,但我也需要結果按此過濾纔是正確的。T-SQL:如何保留左連接但過濾器查詢
查詢:
SELECT
t1.DateFull, COUNT(t2.ContainerWashedDate) as Washes
FROM
DateLookup t1
LEFT JOIN
factContainerWash t2 ON t1.Datefull = t2.ContainerWashedDate
WHERE
(t1.DateFull >= '10/5/2016')
AND (t1.DateFull <= '11/9/2016')
AND t2.createdBy <> 21
GROUP BY
t1.DateFull
ORDER BY
DateFull
結果:
DateFull | Washes
-------------------------+-------
2016-10-05 00:00:00.000 | 1231
2016-10-06 00:00:00.000 | 466
2016-10-10 00:00:00.000 | 84
2016-10-12 00:00:00.000 | 75
預期結果:
DateFull | Washes
-------------------------+-------
2016-10-05 00:00:00.000 | 1231
2016-10-06 00:00:00.000 | 466
2016-10-07 00:00:00.000 | 655
2016-10-08 00:00:00.000 | 23
但'createdBy' **不能**是21,如果它根本不存在。您首先需要解決一個邏輯問題。 – Amit