在SSRS中的值范围之间添加值

时间:2015-06-01 03:36:30

标签: sql reporting-services

我正在创建一个SSRS报告,我有一个一维表,它包含一个字段“No_Of_Stories”和一个带有“Total_TIV”列的事实表。我想要一个以下格式的输出。

Range  | TIV  
1      | 1000  
2 to 3 | 20000  
4 to 7 | 30000

有没有任何方法可以使用SQL查询本身来解决这个问题,或者SSRS中是否有任何表达式来解决这个问题?

1 个答案:

答案 0 :(得分:1)

SELECT 
  CASE 
    WHEN No_Of_Stories = 1 THEN '1'
    WHEN No_Of_Stories >= 2 AND No_Of_Stories < 4 THEN '2 to 3'
    WHEN No_Of_Stories >= 4 AND No_Of_Stories < 8 THEN '4 to 7'
    -- Add any other ranges here
    ELSE 'More than 7' -- or whatever the maximum range is
  END As Range,
  SUM(TIV) AS Total_TIV
FROM MyTable
GROUP BY 
  CASE 
    WHEN No_Of_Stories = 1 THEN '1'
    WHEN No_Of_Stories >= 2 AND No_Of_Stories < 4 THEN '2 to 3'
    WHEN No_Of_Stories >= 4 AND No_Of_Stories < 8 THEN '4 to 7'
    -- Add any other ranges here
    ELSE 'More than 7' -- or whatever the maximum range is
  END
ORDER BY Max(No_Of_Stories)