所以,我有一个在WHERE
和HAVING
子句中使用GETDATE()的查询:
SELECT GETDATE(), COUNT(*) FROM (
SELECT 1 FROM events
WHERE (event_time > (GETDATE() - interval '25 hours'))
GROUP BY id
HAVING MAX(event_time) BETWEEN (GETDATE() - interval '25 hours') AND (GETDATE() - interval '24 hours')
)
我基本上试图找到25到24小时前最新id
的唯一event_time
个数。相对于当前时间。
问题:我有另一个表query_dts
,其中包含一个包含时间戳的列。我没有使用GETDATE()
在当前时间运行上述查询,而是需要运行query_dts
表的每个条目的时间戳。有什么想法吗?
注意:我实际上并没有将query_dts
存储在任何地方。我是这样创建的:
WITH query_dts AS (
SELECT (
DATEADD(hour,-(row_number() over (order by true)), getdate())
) as n
FROM events LIMIT 48
),
我从here
获得的答案 0 :(得分:2)
如何完全避开生成器,而只是分割间隔:
SELECT
dateadd(hour, -distance, getdate()),
count(0) AS event_count
FROM (
SELECT
id,
datediff(hour, max(event_time), getdate()) AS distance
FROM events
WHERE event_time > getdate() - INTERVAL '2 days'
GROUP BY id) AS events_with_distance
GROUP BY distance;
答案 1 :(得分:0)
您可以使用JOIN
来合并这两个查询。然后,您只需要替换日期表达式的值。我认为这是逻辑:
WITH query_dts AS (
SELECT DATEADD(hour, -(row_number() over (order by true)), getdate()) as n
FROM events
LIMIT 48
)
SELECT d.n, COUNT(*)
FROM (SELECT d.n
FROM events e JOIN
query_dts d
WHERE e.event_time > d.n
GROUP BY id
HAVING MAX(event_time) BETWEEN n - interval '25 hours' AND n
) i;
答案 2 :(得分:0)
这是我最终做的事情:
WITH max_time_table AS
(
SELECT id, max(event_time) AS max_time
FROM events
WHERE (event_time > GETDATE() - interval '74 hours')
GROUP BY id
),
query_dts AS
(
SELECT (DATEADD(hour,-(row_number() over (ORDER BY TRUE) - 1), getdate()) ) AS n
FROM events LIMIT 48
)
SELECT query_dts.n, COUNT(*)
FROM max_time_table JOIN query_dts
ON max_time_table.max_time BETWEEN (query_dts.n - interval '25 hours') AND (query_dts.n - interval '24 hours')
GROUP BY query_dts.n
ORDER BY query_dts.n DESC
在这里,我选择了74小时,因为我想要48小时前+ 25小时前= 73小时前。
问题在于,这不是一种通用的方法。对于这个特定问题,它是一个非常具体的解决方案。有人能想到使用另一个表中的日期列来运行依赖于GETDATE()
的查询的更一般方法吗?