1

我想从列的条件计数中为 SSRS 提供 5 个计数。例如,假设该列包含产品的颜色——绿色、蓝色、红色和黄色。我想做的是在单个查询中返回每个的计数。

尽管我可以使用 case 语句完成此操作:

Select
      COUNT(*) 'Count',
      case
            When Color = 'BL' then 'Blue
            When Color = 'RD' then 'Red
            When Color = 'YL' then 'Yellow
            When Color = 'GR' then 'Green
            Else 'All Others'
      End as Payment
From COLORS(NoLock)
Group by
      case
            When Color = 'BL' then 'Blue
            When Color = 'RD' then 'Red
            When Color = 'YL' then 'Yellow
            When Color = 'GRthen ‘Green’
            Else 'All Others'
      End

当我使用数据集是 SSRS 时,我得到的只是一个计数。我不想创建 4 个数据集查询,因为我实际上是通过参数开始和结束日期选择记录,我最终会得到 5 组日期参数。

4

1 回答 1

5

这应该可以解决问题

select count (*) as Total,
   sum (case when color='BL' then 1 else 0 end) as BlueTotal,
   sum (case when color='RD' then 1 else 0 end) as RedTotal,
   sum (case when color='YL' then 1 else 0 end) as YellowTotal,
   sum (case when color='GR' then 1 else 0 end) as GreenTotal
from Colors
于 2012-11-05T20:04:36.963 回答