SSRS - 根据用户选项显示数据报告方式:(小时/天/月/年/日峰)

时间:2017-03-29 18:01:24

标签: sql-server-2008 reporting-services ssrs-2012

我有一个DB表,用于存储customername,每小时值(0-23)和所有米的读取日期。我创建了一份SSRS报告,根据日期范围(开始和结束日期)从表中获取每小时数据

报告设计具有以下参数:

Start Date:
End Date:
Customer: Dropdown list that show all our active customers
Report By: Dropdown list that show (Hour/Day/Month/Day Peak/Month Peak/Year Peak) 

最终用户选择日期范围(开始和结束),customername和Reportby:Month。我应该能够根据用户选择的选项显示报告(报告方式:)。

我在查询下创建了一个具有此查询的数据集:

SELECT CustomerName, ReadingDate, IntegratedHour, IntegratedUsage, IntegratedGeneration
FROM         DB.dbo.TableName
WHERE     (DATEADD(HOUR, IntegratedHour, ReadingDate) BETWEEN DATEADD(HOUR, 0, @StartDate) AND DATEADD(HOUR, 23, @EndDate)) AND 
                      (CustomerName = @FacilityName)
ORDER BY ReadingDate, IntegratedHour

此查询会产生每小时值。

如何根据最终用户选择过滤或使其按日/月/年显示报告。我不知道在哪里设置过滤器或如何查询。

有人可以帮我解决这个问题。感谢您的帮助/建议。感谢

1 个答案:

答案 0 :(得分:0)

我不知道您的日峰/月峰/年峰定义是什么,但希望这可以提供帮助。

DECLARE @StartDate DATETIME = '2017-01-01'
      , @EndDate DATETIME = '2017-12-02'
      , @FacilityName VARCHAR(50) = 'Peter'
      , @ReportBY VARCHAR(20) = 'Month'

DECLARE @TestData TABLE (CustomerName VARCHAR(50),ReadingDate DATETIME,IntegratedHour INT,IntegratedUsage DECIMAL(15,2),IntegratedGeneration  DECIMAL(15,2))
INSERT INTO @TestData
SELECT 'Peter','2017-01-01',1,20,33
INSERT INTO @TestData
SELECT 'Peter','2017-01-01',2,21,33
INSERT INTO @TestData
SELECT 'Peter','2017-01-01',3,22,33
INSERT INTO @TestData
SELECT 'Peter','2017-02-01',3,22,33


SELECT CustomerName
     , CASE WHEN @ReportBY = 'Hour' THEN IntegratedHour 
              WHEN @ReportBY = 'Day' THEN ReadingDate
              WHEN @ReportBY = 'Month' THEN MONTH(ReadingDate)
              WHEN @ReportBY = 'Day Peak' THEN ReadingDate
              WHEN @ReportBY = 'Month Peak' THEN ReadingDate
              WHEN @ReportBY = 'Year Peak' THEN ReadingDate
              ELSE  ReadingDate END As DateType
     , SUM(IntegratedUsage)
     , SUM(IntegratedGeneration)
FROM  @TestData
WHERE (DATEADD(hh, IntegratedHour, ReadingDate) BETWEEN DATEADD(hh, 0, @StartDate) AND DATEADD(hh, 23, @EndDate)) 
    AND (CustomerName = @FacilityName)
GROUP BY CustomerName
       , CASE WHEN @ReportBY = 'Hour' THEN IntegratedHour 
              WHEN @ReportBY = 'Day' THEN ReadingDate
              WHEN @ReportBY = 'Month' THEN MONTH(ReadingDate)
              WHEN @ReportBY = 'Day Peak' THEN ReadingDate
              WHEN @ReportBY = 'Month Peak' THEN ReadingDate
              WHEN @ReportBY = 'Year Peak' THEN ReadingDate
              ELSE  ReadingDate END