SQL Server Reporting Services 将小时格式设置为小时:分钟

发布于 2024-09-02 15:41:44 字数 344 浏览 5 评论 0原文

我正在 SQL Server Reporting Server 上编写报告,其中包含按用户分组的小时数以及根据值总和计算的总计。

目前,我的查询运行一个存储过程,它以 HH:MM 格式返回小时,而不是十进制小时,因为我们的用户发现这更直观。当我尝试使用 SSRS 表达式对列进行求和时,就会出现问题,因为 SUM 函数不够智能,无法处理这种格式的求和时间。

有没有办法:

  1. 以 HH:MM 格式显示时间间隔(以分钟或小时为单位),同时以十进制形式计算?
  2. 或者拆分并计算 HH:MM 文本值的总和以得出表达式形式的总和?

我想避免仅仅为了获得总数而编写/运行第二个查询。

I'm writing reports on SQL Server Reporting Server that have a number of hours grouped by, say, user, and a total calculated based on the sum of the values.

Currently my query runs a stored proc that returns the hours as in HH:MM format, rather than decimal hours, as our users find that more intuitive. The problem occurs when I try and add up the column using an SSRS expression, because the SUM function isn't smart enough to handle adding up times in this format.

Is there any way to:

  1. Display a time interval (in minutes or hours) in HH:MM format while having it calculated in decimal form?
  2. Or split up and calculate the total of the HH:MM text values to arrive at a total as an expression?

I'd like to avoid having to write/run a second query just to get the total.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

演出会有结束 2024-09-09 15:41:44

回答我自己的#1:

  1. 让您的查询返回分钟数(由下面的 TimeSpent 表示)。
  2. 如果文本框的格式尚未设置为“常规”,
  3. 请使用以下表达式作为值:
    =FLOOR(Fields!TimeSpent.Value / 60) & “:”& RIGHT("0" & (Fields!TimeSpent.Value MOD 60), 2)
  4. 对于总和文本框,请使用以下表达式:=FLOOR(Sum(Fields!TimeSpent.Value) / 60) & “:”& RIGHT("0" & (Sum(Fields!TimeSpent.Value) MOD 60), 2)

Answering my own #1:

  1. Make your query return a number of minutes (denoted by TimeSpent below).
  2. Set the format of your textbox to "General" if it isn't already
  3. Use the following expression as the value:
    =FLOOR(Fields!TimeSpent.Value / 60) & ":" & RIGHT("0" & (Fields!TimeSpent.Value MOD 60), 2)
  4. For the sum textbox, use the following expression: =FLOOR(Sum(Fields!TimeSpent.Value) / 60) & ":" & RIGHT("0" & (Sum(Fields!TimeSpent.Value) MOD 60), 2)
成熟稳重的好男人 2024-09-09 15:41:44

您也可以尝试将此函数放入报告的自定义代码中。

Function secondsToString(seconds As int64) As String
Dim myTS As New TimeSpan(seconds * 10000000)
Return String.Format("{0:00}:{1:00}:{2:00}",myTS.Hours, myTS.Minutes, myTS.Seconds)
End Function

我的 SP 通常以秒为单位返回时间,如果我必须在多个地方返回 HH:MM:SS,这使我不必费力思考。另外,您可以正常执行所有聚合并将它们包装在其中以获得漂亮的格式。

我不能将此归功于我,因为我知道我不久前在网上偶然发现了它,但我不记得在哪里。

You may also try putting this function in your report's custom code.

Function secondsToString(seconds As int64) As String
Dim myTS As New TimeSpan(seconds * 10000000)
Return String.Format("{0:00}:{1:00}:{2:00}",myTS.Hours, myTS.Minutes, myTS.Seconds)
End Function

My SPs generally return time in seconds and this keeps me from having to think too hard if I have to return HH:MM:SS in more than one place. Plus, you can do all of your aggregations normally and wrap them in this to get a pretty format.

I can't take credit for this as I know I stumbled across it on the web some time ago, but I can't recall where.

赠意 2024-09-09 15:41:44

因此,在您的情况下,它是分钟,因此您需要将其转换为秒,您可以尝试使用以下代码:

=Format(DateAdd("s",(Parameters!ReportParameter1.Value * 60), "00:00:00"), “HH:mm:ss”)

希望这有帮助

So in your case its minutes so you need to convert it to seconds , you can try using below code:

=Format(DateAdd("s",(Parameters!ReportParameter1.Value * 60), "00:00:00"),"HH:mm:ss")

Hope this helps

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