[英]How to use count inside case
請幫助我用案例陳述來計數。
我想要這個結果:
Equal : 50
GT : 25
LT : 15
下面是我的代碼:
select Input,
CASE
when math = '=' then
count(case when Input = UDTarget then Input else 0 end)
end as Equals,
CASE
when math = '>' then
count(case when ISNUMERIC(Input) < ISNUMERIC(UDTarget) then Input else 0 end)
end as GT,
CASE when math = '<' then
count(case when ISNUMERIC(Input) > ISNUMERIC(UDTarget) then Input else 0 end)
END as LT
FROM [NEWSEMAKPI].[dbo].[NewCriteria] NC
inner join [NEWSEMAKPI].[dbo].[UpdateData] UD ON UD.Cid = NC.Id
where InputWeek='15'
group by Input
之所以顯示您的錯誤,是因為您沒有在group by
子句中添加math
。 因此,您應該將其放入其中:
select Input,
CASE
when math = '=' then
count(case when Input = UDTarget then Input else 0 end)
end as Equals,
CASE
when math = '>' then
count(case when ISNUMERIC(Input) < ISNUMERIC(UDTarget) then Input else 0 end)
end as GT,
CASE when math = '<' then
count(case when ISNUMERIC(Input) > ISNUMERIC(UDTarget) then Input else 0 end)
END as LT
FROM [NEWSEMAKPI].[dbo].[NewCriteria] NC
inner join [NEWSEMAKPI].[dbo].[UpdateData] UD ON UD.Cid = NC.Id
where InputWeek='15'
group by Input, math
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.