关于水晶报表的问题+使用 GROUP BY 的 SQL Server 存储过程

发布于 2024-11-14 11:27:49 字数 734 浏览 4 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离不开的别离 2024-11-21 11:27:49

以下是我解决这个问题的方法:

  1. 在 SQL Server 中创建一个本地表。称之为“LU_Hours”。
  2. 该表将有 1 个整数字段(称为“小时”),共 24 行。当然,值可以是 1 到 24。
  3. 将其右连接到现有查询中。
  4. 您可能需要对此进行调整,以确保对空白时间的处理达到您满意的程度。

Here's how I solved this problem:

  1. Create a local table in your SQL-Server. Call it "LU_Hours".
  2. This table will have 1 integer field (called "Hours") with 24 rows. Of course, the values would be 1 through 24.
  3. Right join this onto your existing query.
  4. You might need to tweak this to make sure the nulls of empty hours are handled to your satisfaction.
赴月观长安 2024-11-21 11:27:49

您可以使用WITH 子句创建24 小时,然后对其进行OUTER JOIN。

WITH hours AS (

  SELECT 1 AS hour
  UNION
  SELECT 2 AS hour
  ...
  SELECT 24 AS hour

)

SELECT *
FROM hours h
LEFT OUTER JOIN [your table] x ON h.hour=x.datepart(hh, convert(smalldatetime, convert(float, Timestamp) / 1440 - 1))

该 SQL 需要添加到命令中。

根据需要在 SQL 或报告中进行分组。

You could use a WITH clause to create the 24 hours, then OUTER JOIN it.

WITH hours AS (

  SELECT 1 AS hour
  UNION
  SELECT 2 AS hour
  ...
  SELECT 24 AS hour

)

SELECT *
FROM hours h
LEFT OUTER JOIN [your table] x ON h.hour=x.datepart(hh, convert(smalldatetime, convert(float, Timestamp) / 1440 - 1))

This SQL would need to added to a Command.

Group as necessary in the SQL or the report.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文