关于水晶报表的问题+使用 GROUP BY 的 SQL Server 存储过程
我正在 Crystal Reports XI Developer 中编写一个报表,该报表在 SQL Server 2005 数据库中运行一个存储过程。记录集返回按天和小时分组的日志表的摘要。
目前我的查询看起来像这样:
SELECT
sum(colA) as "Total 1",
day(convert(smalldatetime, convert(float, Timestamp) / 1440 - 1)) as "Date",
datepart(hh, convert(smalldatetime, convert(float, Timestamp) / 1440 - 1)) as "Hour"
`etc...`
GROUP BY
Day, Hour
忽略日期疯狂,我认为系统设计者在研究如何存储日期时喝得很厉害。
我的问题是这样的:由于并不总是有一天中每个小时的记录,所以我最终会出现间隙,这是可以理解的,但我希望 Crystal 能够报告整个 24 小时,无论是否有数据与否。
我知道我可以通过将整个查询放入 WHILE
循环(在存储过程内)并在各个小时内执行查询来更改此设置,但我内心深处的感觉告诉我,一个查询比 24 个查询更好。
我我想知道是否有一种方法可以让 Crystal 遍历一天中的各个时间,而不是像通常那样遍历表中的行。
或者,有没有一种方法可以格式化查询,使其包含空的小时行,而不会杀死我的数据库服务器?
I'm writing a report in Crystal Reports XI Developer that runs a stored procedure in a SQL Server 2005 database. The record set returns a summary from log table grouped by Day and Hour.
Currently my query looks something like this:
SELECT
sum(colA) as "Total 1",
day(convert(smalldatetime, convert(float, Timestamp) / 1440 - 1)) as "Date",
datepart(hh, convert(smalldatetime, convert(float, Timestamp) / 1440 - 1)) as "Hour"
`etc...`
GROUP BY
Day, Hour
Ignore the date insanity, I think the system designers were drinking heavily when they worked out how to store their dates.
My problem is this: since there are not always records from each hour of the day, then I end up with gaps, which is understandable, but I'd like Crystal to be able to report on the entire 24 hours regardless of whether there is data or not.
I know I can change this by putting the entire query in a WHILE
loop (within the stored procedure) and doing queries on the individual hours, but something inside me says that one query is better than 24.
I'd like to know if there's a way to have Crystal iterate through the hours of the day as opposed to iterating through the rows in the table as it normally does.
Alternatively, is there a way to format the query so that it includes the empty hour rows without killing my database server?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下是我解决这个问题的方法:
Here's how I solved this problem:
您可以使用WITH 子句创建24 小时,然后对其进行OUTER JOIN。
该 SQL 需要添加到命令中。
根据需要在 SQL 或报告中进行分组。
You could use a WITH clause to create the 24 hours, then OUTER JOIN it.
This SQL would need to added to a Command.
Group as necessary in the SQL or the report.