Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BigQuery Where Date is Less Than or Equal to 3 Days Minus Current Date

I'm trying to create a query to only return data where date is minus 3 days from the current date. I've tried:

date <= DATE_ADD(CURRENT_DATE(), -3, 'DAY')

But this returns Error: Expected INTERVAL expression

like image 794
ericbrownaustin Avatar asked Apr 24 '17 23:04

ericbrownaustin


2 Answers

This works with a string formatted date.

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

like image 167
ericbrownaustin Avatar answered Sep 22 '22 14:09

ericbrownaustin


See WHERE clause in below example

#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

Btw, in case if you are still with Legacy SQL - see below example

#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
like image 33
Mikhail Berlyant Avatar answered Sep 21 '22 14:09

Mikhail Berlyant