BigQuery其中日期小于或等于3天减去当前日期

时间:2017-04-24 23:54:46

标签: google-bigquery

我尝试创建一个查询,只返回日期距当前日期减去3天的数据。我试过了:

Error: Expected INTERVAL expression

但这会返回def line(word, filename): f = dict(list(enumerate(open(filename,'r')))) return [k for k in f if word in f[k]]

2 个答案:

答案 0 :(得分:6)

参见以下示例中的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

答案 1 :(得分:1)

这适用于字符串格式化的日期。

DATE(TIMESTAMP(date)) <= DATE_SUB(CURRENT_DATE(), INTERVAL 3 DAY)