我的查詢是獲取過去5周的數據。問題與連接在我的查詢,如何填充空值爲零
select z.week,
sum(case when i.severity=1 then 1 else 0 end) as 1
sum(case when i.severity=2 then 1 else 0 end) as 2
sum(case when i.severity=3 then 1 else 0 end) as 3
sum(case when i.severity=4 then 1 else 0 end) as 4
from instance as i
and left outer join year as z on convert(varchar(10),z.date,101)=convert(varchar(10),i.created,101)
and left outer join year as z on convert(varchar(10),z.date,101)=convert(varchar(10),i.closed,101)
where i.group in '%Teams%'
and z.year=2013
and z.week<=6 and z.week>1
這裏有幾個星期在我的實例表中,甚至沒有一行。所以這裏我沒有得到空或零...而是整個行並沒有提示。
我目前的輸出。
week | 1 | 2 | 3 | 4
---------------------
2 | 0 | 1 | 8 | 5
3 | 2 | 3 | 4 | 9
5 | 1 | 0 | 0 | 0
,但我需要像下面的輸出...
week | 1 | 2 | 3 | 4
---------------------
2 | 0 | 1 | 8 | 5
3 | 2 | 3 | 4 | 9
4 | 0 | 0 | 0 | 0
5 | 1 | 0 | 0 | 0
6 | 0 | 0 | 0 | 0
如何獲得所需的outputiñSQL
你使用的是什麼風格的sql? – 2013-03-08 22:40:18
使用左連接而不是左外連接 – Kyra 2013-03-08 22:42:14
@Kyra都相同 – 2013-03-08 22:44:56