英文:
SSRS Plot an hourly graph
问题
我正在尝试生成一个根据小时分组的区域图,以数量为单位。我的数据集查询如下:
SELECT
FORMAT(DATEADD(hh, DATEPART(hour, TRANDATETIME), '00:00'), 'HH:mm') AS SalesByHour,
SUM(TQty) as SalesQty
FROM
[RC_BACK].[dbo].[TRAN]
WHERE TRANCODE = 'SA'
GROUP BY FORMAT(DATEADD(hh, DATEPART(hour, TRANDATETIME), '00:00'), 'HH:mm')
我得到了类似下面的结果:
SalesbyHour | SalesQty |
---|---|
14:00 | 200 |
17:00 | 5000 |
21:00 | 8000 |
23:00 | 20000 |
我想生成一个包含没有数量的小时并且在水平轴上有1小时间隔的图表。
这是我到目前为止尝试的内容:
"水平轴属性.." -> "标量" -> "最小值: =TimeValue("00:00") 最大值: =TimeValue("23:59") 间隔: 1 间隔类型: 小时" -> "数字" -> "类别: 时间 类型: 13:30" -> "主要刻度线标记" -> "间隔类型: 小时"
我成功调整了水平轴,但在预览图中,值没有绘制出来,如下所示:
这些值应该具有与x轴标签相同的数据类型,但没有绘制在图表上,我希望没有数量的小时也在图表上以0绘制出来。或者是否可能通过查询来填充SalesQty中剩余的小时为0?我已经查看了很多问题和答案,但还没有成功。
英文:
I am trying to generate an area graph with quantity grouped by hours. My dataset query is as below:
SELECT
FORMAT(DATEADD(hh,DATEPART(hour, TRANDATETIME),'00:00'),'HH:mm') AS SalesByHour,
SUM(TQty) as SalesQty
FROM
[RC_BACK].[dbo].[TRAN]
WHERE TRANCODE = 'SA'
GROUP BY FORMAT(DATEADD(hh,DATEPART(hour, TRANDATETIME),'00:00'),'HH:mm')
and I got the results with something like below:
SalesbyHour | SalesQty |
---|---|
14:00 | 200 |
17:00 | 5000 |
21:00 | 8000 |
23:00 | 20000 |
I want to generate a graph that includes the hours that have no quantity and would have 24 hours with 1 hour interval in the horizontal axis.
This is what I've tried so far
"Horizontal axis properties.." -> "Scalar" -> "Minimum: =TimeValue("00:00") Maximum: =TimeValue("23:59") Interval:1 Interval Type: Hours" -> "Number" -> "Category: Time Type: 13:30" -> "Major Tick Marks" -> "Interval Type: Hours"
and I managed to get the horizontal axis right but the values are not plotting on the graph in preview as below:
The values should have the same data type as the x-axis label but it is not plotted and I would like to have the hours with no quantity plotted with 0 on the graph as well. Or is it possible to fill in the remaining hours with 0 in SalesQty through query? I have look through a lot of questions and answers but still no luck yet.
答案1
得分: 1
这应该根据您现在的情况可以工作。我所做的只是创建了一个小时表,始终从该表返回小时,即使在您的原始表中没有匹配的值。这可以很容易地改进,但由于我不知道您表的结构,这种方法是最简单的。
DECLARE @hours TABLE (hr as varchar(5))
INSERT INTO @hours VALUES
('00:00'), ('01:00'), ('02:00'), ('03:00'), ('04:00'), ('05:00')
, ('06:00'), ('07:00'), ('08:00'), ('09:00'), ('10:00'), ('11:00')
, ('12:00'), ('13:00'), ('14:00'), ('15:00'), ('16:00'), ('17:00')
, ('18:00'), ('19:00'), ('20:00'), ('21:00'), ('22:00'), ('23:00')
SELECT h.hr as SalesByHour, ISNULL(d.SalesQty, 0) as SalesQty
FROM @hours h
LEFT JOIN
(
SELECT
FORMAT(DATEADD(hh,DATEPART(hour, TRANDATETIME),'00:00'),'HH:mm') AS SalesByHour,
SUM(TQty) as SalesQty
FROM [RC_BACK].[dbo].[TRAN]
WHERE TRANCODE = 'SA'
GROUP BY FORMAT(DATEADD(hh,DATEPART(hour, TRANDATETIME),'00:00'),'HH:mm')
) d ON h.hr = d.SalesByHour
然而,我可能会建议避免在查询中进行所有时间格式的处理,而只传递实际时间,这样轴间隔上的时间属性将按预期工作。由于您已经接近目标,可能不值得花费太多精力,但未来请记住这一点。
英文:
This should work based on what you have now, All I've done is create a table of the hours and always returned the hour from that table even if there is no matching value in your original table. This could easily be improved but as I don't know the structure of your tables, this method was the easiest.
DECLARE @hours TABLE (hr as varchar(5))
INSERT INTO @hours VALUES
('00:00'), ('01:00'), ('02:00'), ('03:00'), ('04:00'), ('05:00')
, ('06:00'), ('07:00'), ('08:00'), ('09:00'), ('10:00'), ('11:00')
, ('12:00'), ('13:00'), ('14:00'), ('15:00'), ('16:00'), ('17:00')
, ('18:00'), ('19:00'), ('20:00'), ('21:00'), ('22:00'), ('23:00')
SELECT h.hr as SalesByHour, ISNULL(d.SalesQty, 0) as SalesQty
FROM @hours h
LEFT JOIN
(
SELECT
FORMAT(DATEADD(hh,DATEPART(hour, TRANDATETIME),'00:00'),'HH:mm') AS SalesByHour,
SUM(TQty) as SalesQty
FROM [RC_BACK].[dbo].[TRAN]
WHERE TRANCODE = 'SA'
GROUP BY FORMAT(DATEADD(hh,DATEPART(hour, TRANDATETIME),'00:00'),'HH:mm')
) d ON h.hr = d.SalesByHour
However, I would probably avoid doing all the formatting of time in your query and just pass an actual time, that way the time properties on the axis intervals would work as intended. As you are almost there it might not be worth the effort, but worth bearing in mind in the future.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论