简体   繁体   中英

SSRS. How to group in a group?

I have SSRS report like below with Boolean parameter to show 12h view or 24h view. To fit report into single screen the 24h report need to group by every 2hr.

          07:00 08:00 09:00 10:00 11:00 12:00 13:00 14:00 ...
Line 1     25    30    24    26    25    25    30    30   ...

          08:00 10:00 12:00 14:00 ...
Line 1     55    50    50    60   ...

The query for the dataset is:

SELECT LineID
  ,Hour
  ,HourValue
  ,Target
FROM vwData
ORDER BY LineID, CASE WHEN [Hour] > 6 THEN - 1 ELSE [Hour] END

How can I achieve this?

This declares your bit variable (which should be true when they want the 24 hour view - false when 12 hour)

DECLARE @24Hour bit = 0

SELECT CASE WHEN @24Hour = 0
    THEN Hour
    ELSE Hour + (Hour % 2)
    END AS [HourGroup]
  ,SUM(Target) AS [TargetTotal]
FROM vwData
GROUP BY CASE WHEN @24Hour = 0
    THEN Hour
    ELSE Hour + (Hour % 2)
    END

If they want the 24 hour view, we make hour = hour + hour % 2. (7 = 8, 8=8, 9=10, etc., etc.). If you had a more complex query, I would suggest reading up on cross apply, but this is so simple I think this will suffice. The grouping by makes sure to aggregate the REAL 7 and REAL 8 hour records (which will both be returned as "8", if using the 24 hour view). If you don't group your results, you will get two 8 oclock records - one with the REAL 7 hour total and one with the REAL 8 hour total.

EDIT:

Since you didn't include the schema of your DB, I'm guessing that 'Target' is the value being summated, but it could just as easily be 'HourValue'. Furthermore, I have no idea why you would need LineID, so I omitted it from my answer. But you can easily modify that if it's inaccurate. In the future, you should provide some sample data and your database schema so that others aren't forced to make assumptions or guess.

您可以添加一个具有如下所示值的计算字段:“ Fields!Hour.Value + Fields!Hour.Value Mod 2”,然后对该字段进行分组,并使用参数选择报告中的“分组依据”字段(您的新字段或实际小时值)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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