繁体   English   中英

内盒如何使用计数

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM