[英]How to use count inside case
Please help me to count using case statement. 请帮助我用案例陈述来计数。
I want this result: 我想要这个结果:
Equal : 50
GT : 25
LT : 15
Below is my code: 下面是我的代码:
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
Your error is shown because you didn't put math
in your group by
clause. 之所以显示您的错误,是因为您没有在group by
子句中添加math
。 So you should put it inside: 因此,您应该将其放入其中:
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.