0

我已经搜索过以前的帖子,但找不到我正在寻找的答案。我想在 24 小时内间隔 30 分钟,例如

开始 = 00:30、01:00、01:30、02:00 等 结束 - 24:00

如果你能指出我正确的方向。这可以使用 Web 智能查询中的自定义查询脚本来完成吗?

谢谢

4

1 回答 1

0

我不确定如何在 WebI 中本地执行此操作,但您当然可以在徒手 SQL 查询中强制执行此操作。我使用的数据库是 SQL Server。您的 SQL 语法可能因您的数据库平台而异。

最简单的方法是一堆工会......

SELECT '00:30' AS TimeInterval
UNION SELECT '01:00'
UNION SELECT '01:30'
UNION SELECT '02:00'
UNION SELECT '02:30'
UNION SELECT '03:00'
UNION SELECT '03:30'
UNION SELECT '04:00'
UNION SELECT '04:30'
UNION SELECT '05:00'
UNION SELECT '05:30'
UNION SELECT '06:00'
UNION SELECT '06:30'
UNION SELECT '07:00'
UNION SELECT '07:30'
UNION SELECT '08:00'
UNION SELECT '08:30'
UNION SELECT '09:00'
UNION SELECT '09:30'
UNION SELECT '10:00'
UNION SELECT '10:30'
UNION SELECT '11:00'
UNION SELECT '11:30'
UNION SELECT '12:00'
UNION SELECT '12:30'
UNION SELECT '13:00'
UNION SELECT '13:30'
UNION SELECT '14:00'
UNION SELECT '14:30'
UNION SELECT '15:00'
UNION SELECT '15:30'
UNION SELECT '16:00'
UNION SELECT '16:30'
UNION SELECT '17:00'
UNION SELECT '17:30'
UNION SELECT '18:00'
UNION SELECT '18:30'
UNION SELECT '19:00'
UNION SELECT '19:30'
UNION SELECT '20:00'
UNION SELECT '20:30'
UNION SELECT '21:00'
UNION SELECT '21:30'
UNION SELECT '22:00'
UNION SELECT '22:30'
UNION SELECT '23:00'
UNION SELECT '23:30'
UNION SELECT '24:00'

这是使用 CTE 的更优雅的方法...

DECLARE @startnum INT = 1;
DECLARE @endnum INT = 48;
WITH cte AS
(
    SELECT @startnum AS num
    UNION ALL
    SELECT num + 1 FROM cte WHERE num + 1 <= @endnum
)
SELECT CASE
           WHEN num < 48 THEN
                -- Here is the logical progression working from the inside out
                -- 1. convert GETDATE() to a DATE to get rid of the time
                -- 2. convert the result of #1 back to a DATETIME
                -- 3. add the appropriate number of minutes
                -- 4. convert to VARCHAR(5) style of 8 to get hh:mi
               CONVERT(VARCHAR(5), DATEADD(MINUTE, num * 30, CONVERT(DATETIME, CONVERT(DATE, GETDATE()))), 8)
           ELSE
               '24:00'
       END AS TimeInterval
FROM   cte;

希望您可以使其中一项为您工作。

于 2019-12-10T16:52:10.057 回答