我想創建一個查詢,以返回數據,其中日期是從當前日期減去3天。我已經試過:BigQuery當日期小於或等於3天減去當前日期
date <= DATE_ADD(CURRENT_DATE(), -3, 'DAY')
但這返回Error: Expected INTERVAL expression
我想創建一個查詢,以返回數據,其中日期是從當前日期減去3天。我已經試過:BigQuery當日期小於或等於3天減去當前日期
date <= DATE_ADD(CURRENT_DATE(), -3, 'DAY')
但這返回Error: Expected INTERVAL expression
見WHERE子句中下面的例子
#standardSQL
WITH yourTable AS (
SELECT i, date
FROM UNNEST(GENERATE_DATE_ARRAY('2017-04-15', '2017-04-28')) AS date WITH OFFSET AS i
)
SELECT *
FROM yourTable
WHERE date <= DATE_SUB(CURRENT_DATE(), INTERVAL 3 DAY)
-- ORDER BY date
順便說一下,萬一如果你還在用傳統的SQL - 見例如下面
#legacySQL
SELECT *
FROM -- yourTable
(SELECT 1 AS id, DATE('2017-04-20') AS date),
(SELECT 2 AS id, DATE('2017-04-21') AS date),
(SELECT 3 AS id, DATE('2017-04-22') AS date),
(SELECT 4 AS id, DATE('2017-04-23') AS date),
(SELECT 5 AS id, DATE('2017-04-24') AS date),
(SELECT 6 AS id, DATE('2017-04-25') AS date)
WHERE TIMESTAMP(date) <= DATE_ADD(TIMESTAMP(CURRENT_DATE()), -3, 'DAY')
-- ORDER BY date
這適用於字符串格式化的日期。
DATE(TIMESTAMP(date)) <= DATE_SUB(CURRENT_DATE(), INTERVAL 3 DAY)