如何过滤 "date" - 2 周和 "date" + 2 周的日期范围
How to filter date range from "date" - 2 weeks and "date" + 2 weeks
我有一个 table 喜欢:
VALID_FROM | VALID_TO | stock
2020-10-01 | 2020-10-02 | 10
2020-10-02 | 2020-10-04 | 9
2020-10-04 | 2020-10-08 | 5
... | ... |
2020-12-15 | 2020-12-16 | 0
我需要为特定日期设置日期范围:
'2020-11-26' - 2 周/14 天
'2020-11-26' + 2 周/14 天
我可以只使用固定日期,例如
SELECT *
FROM table AS t
WHERE t.VALID_FROM >= '2020-11-12'
AND t.VALID_TO <= '2020-12-10'
但是日期范围可能会改变,我只想添加天数或周数,而不必每次都使用日历。
所以像这样:
SELECT *
FROM table AS t
WHERE t.VALID_FROM >= '2020-11-26' - INTERVAL '14' DAY
AND t.VALID_TO <= '2020-11-26' + INTERVAL '14' DAY
但这给了我错误信息:
Feature not supported: Cannot cast from INTERVAL DAY(2) TO SECOND(3) to DOUBLE
尝试使用 add_days()
函数:
WHERE t.VALID_FROM >= add_days('2020-11-26', -14) AND
t.VALID_TO <= add_days('2020-11-26', -14)
我有一个 table 喜欢:
VALID_FROM | VALID_TO | stock
2020-10-01 | 2020-10-02 | 10
2020-10-02 | 2020-10-04 | 9
2020-10-04 | 2020-10-08 | 5
... | ... |
2020-12-15 | 2020-12-16 | 0
我需要为特定日期设置日期范围:
'2020-11-26' - 2 周/14 天 '2020-11-26' + 2 周/14 天
我可以只使用固定日期,例如
SELECT *
FROM table AS t
WHERE t.VALID_FROM >= '2020-11-12'
AND t.VALID_TO <= '2020-12-10'
但是日期范围可能会改变,我只想添加天数或周数,而不必每次都使用日历。
所以像这样:
SELECT *
FROM table AS t
WHERE t.VALID_FROM >= '2020-11-26' - INTERVAL '14' DAY
AND t.VALID_TO <= '2020-11-26' + INTERVAL '14' DAY
但这给了我错误信息:
Feature not supported: Cannot cast from INTERVAL DAY(2) TO SECOND(3) to DOUBLE
尝试使用 add_days()
函数:
WHERE t.VALID_FROM >= add_days('2020-11-26', -14) AND
t.VALID_TO <= add_days('2020-11-26', -14)