在SSRS DataSet中为单个列返回4个单独的计数

时间:2012-11-05 19:52:08

标签: sql sql-server reporting-services

我想根据列上的条件计数向SSRS提供5个计数。例如,假设列保持产品的颜色 - 绿色,蓝色,红色和黄色。我想做的是在一个查询中返回每个查询的计数。

虽然我可以使用案例陈述完成这项工作:

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组日期参数。

1 个答案:

答案 0 :(得分: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