我有一个每5分钟收集一次等待统计数据的表,
它还有一些其他列的开始和结束列,
我需要查询表格给我一些读数,比如5小时前,10小时前..
我尝试选择使用两者之间但是给了我空白数据
请提示
答案 0 :(得分:0)
如果你的桌子有一些类似的结构:
ts_start |ts_end |otherstuff
2017-01-14 20:00:01|2017-01-14 20:00:02|other stuff 01
2017-01-14 20:00:02|2017-01-14 20:00:03|other stuff 02
2017-01-14 20:00:03|2017-01-14 20:00:04|other stuff 03
2017-01-14 20:00:04|2017-01-14 20:00:05|other stuff 04
2017-01-14 20:00:05|2017-01-14 21:00:01|other stuff 05
2017-01-14 21:00:01|2017-01-14 21:00:02|other stuff 06
2017-01-14 21:00:02|2017-01-14 21:00:03|other stuff 07
2017-01-14 21:00:03|2017-01-14 21:00:04|other stuff 08
2017-01-14 21:00:04|2017-01-14 21:00:05|other stuff 09
2017-01-14 21:00:05|2017-01-14 22:00:01|other stuff 10
2017-01-14 22:00:01|2017-01-14 22:00:02|other stuff 11
2017-01-14 22:00:02|2017-01-14 22:00:03|other stuff 12
2017-01-14 22:00:03|2017-01-14 22:00:04|other stuff 13
2017-01-14 22:00:04|2017-01-14 22:00:05|other stuff 14
2017-01-14 22:00:05|9999-12-31 23:59:59|other stuff 15
...这就是你要找的东西吗? (我不得不模仿CURRENT_TIMESTAMP,否则这将在几个小时内无法工作,而我正试图获取我在一小时内发现的所有事件以获得相关内容......)
WITH foo (ts_start,ts_end,otherstuff) AS (
SELECT TIMESTAMP '2017-01-14 20:00:01',TIMESTAMP '2017-01-14 20:00:02','other stuff 01'
UNION ALL SELECT TIMESTAMP '2017-01-14 20:00:02',TIMESTAMP '2017-01-14 20:00:03','other stuff 02'
UNION ALL SELECT TIMESTAMP '2017-01-14 20:00:03',TIMESTAMP '2017-01-14 20:00:04','other stuff 03'
UNION ALL SELECT TIMESTAMP '2017-01-14 20:00:04',TIMESTAMP '2017-01-14 20:00:05','other stuff 04'
UNION ALL SELECT TIMESTAMP '2017-01-14 20:00:05',TIMESTAMP '2017-01-14 21:00:01','other stuff 05'
UNION ALL SELECT TIMESTAMP '2017-01-14 21:00:01',TIMESTAMP '2017-01-14 21:00:02','other stuff 06'
UNION ALL SELECT TIMESTAMP '2017-01-14 21:00:02',TIMESTAMP '2017-01-14 21:00:03','other stuff 07'
UNION ALL SELECT TIMESTAMP '2017-01-14 21:00:03',TIMESTAMP '2017-01-14 21:00:04','other stuff 08'
UNION ALL SELECT TIMESTAMP '2017-01-14 21:00:04',TIMESTAMP '2017-01-14 21:00:05','other stuff 09'
UNION ALL SELECT TIMESTAMP '2017-01-14 21:00:05',TIMESTAMP '2017-01-14 22:00:01','other stuff 10'
UNION ALL SELECT TIMESTAMP '2017-01-14 22:00:01',TIMESTAMP '2017-01-14 22:00:02','other stuff 11'
UNION ALL SELECT TIMESTAMP '2017-01-14 22:00:02',TIMESTAMP '2017-01-14 22:00:03','other stuff 12'
UNION ALL SELECT TIMESTAMP '2017-01-14 22:00:03',TIMESTAMP '2017-01-14 22:00:04','other stuff 13'
UNION ALL SELECT TIMESTAMP '2017-01-14 22:00:04',TIMESTAMP '2017-01-14 22:00:05','other stuff 14'
UNION ALL SELECT TIMESTAMP '2017-01-14 22:00:05',TIMESTAMP '9999-12-31 23:59:59','other stuff 15'
)
, current_timestamp_mimick(current) AS (
SELECT TIMESTAMP '2017-01-15 01:00:00'
)
SELECT
current AS "current_timestamp"
, *
FROM foo, current_timestamp_mimick
WHERE current - INTERVAL '3 HOURS' >= ts_start
AND current - INTERVAL '4 HOURS' < ts_end
;
current_timestamp |ts_start |ts_end |otherstuff |current
2017-01-15 01:00:00 |2017-01-14 20:00:05 |2017-01-14 21:00:01 |other stuff 05|2017-01-15 01:00:00
2017-01-15 01:00:00 |2017-01-14 21:00:01 |2017-01-14 21:00:02 |other stuff 06|2017-01-15 01:00:00
2017-01-15 01:00:00 |2017-01-14 21:00:02 |2017-01-14 21:00:03 |other stuff 07|2017-01-15 01:00:00
2017-01-15 01:00:00 |2017-01-14 21:00:03 |2017-01-14 21:00:04 |other stuff 08|2017-01-15 01:00:00
2017-01-15 01:00:00 |2017-01-14 21:00:04 |2017-01-14 21:00:05 |other stuff 09|2017-01-15 01:00:00
2017-01-15 01:00:00 |2017-01-14 21:00:05 |2017-01-14 22:00:01 |other stuff 10|2017-01-15 01:00:00
开心玩....
Marco the Sane