我有这样的查询,
select * from (
select a.CreatedMonth,Total_Days,case when a.Type = 'Invalid_Question' then 'Invalid'
when a.Type = 'Valid_Question' then 'Valid'
end as Type
from
(
select CreatedMonth,Type,sum(Questions_Count*LastDays) /sum(Questions_Count) as Total_Days
from #t
group by CreatedMonth,Type
) a
)as d pivot(sum(Total_Days) for Type in (Invalid, Valid)) as p
但是在输出中,Valid为null。我必须将它的值从null设置为0.另外,我在哪里修改代码,在case语句或pivot中?
这是我目前的结果,
CreatedMonth Invalid Valid
1 50 32
2 63 47
3 70 NULL
我该怎么做?
答案 0 :(得分:0)
select CreatedMonth
,ISNULL(Invalid,0) AS Invalid
,ISNULL(Valid ,0) AS Valid
from (
select a.CreatedMonth
,Total_Days
,case
when a.[Type] = 'Invalid_Question' then 'Invalid'
when a.[Type] = 'Valid_Question' then 'Valid'
end as [Type]
from
(
select CreatedMonth
,[Type]
,sum(Questions_Count*LastDays) /sum(Questions_Count) as Total_Days
from #t
group by CreatedMonth,[Type]
) a
)as d pivot(sum(Total_Days)
for [Type]
in ([Invalid], [Valid])) as p