开发者

Grouping and summing items in a table using SSRS

I have a SSRS report, and I'm trying to sum up rows conditionally. I have:

11/15/2010 12:14:43 AM | Current Rate | Current Speed | Amount used in that minute (Speed*Rate/60) etc etc etc

I am trying to add all the rows that happened in an hour, so that my report will show:

11/15/2010 | 12 AM - 1 AM | Amount used for that hour (say, 7 gal开发者_开发知识库lons)

I cannot find anywhere how to conditionally sum up a row per hour, or how to get my report to say the above.

Thank you in advance!


Using the following table for testing:

CREATE TABLE `log` (
  `entry_date` datetime DEFAULT NULL,
  `amount_used` int(11) DEFAULT NULL
)

With some test data:

entry_date           amount_used
2010-11-01 10:00:00, 3
2010-11-01 10:30:00, 1
2010-11-01 11:00:00, 6

Use this query to get the date, hour range and total amount used:

SELECT DATE(entry_date) AS entry_date, 
    CONCAT_WS('-',CONVERT(MIN(HOUR(entry_date)), char(2)), CONVERT(MAX(HOUR(entry_date)),CHAR(2))) hours,
    SUM(amount_used) amount_used
FROM (
    SELECT entry_date, SUM(amount_used) AS amount_used 
    FROM log
    GROUP BY DATE(entry_date), HOUR(entry_date)
    ) T;

All the CONCAT/CONVERT stuff is just to get the range of hours in that particular day, as a string. This is the result:

entry_date  hours  amount_used
2010-11-01, 10-11, 10
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜