简体   繁体   中英

BigQuery: How to get data with three different filters applied to same column?

I'm trying to get data for the following conditions in bigquery. 1. Given_Day -> Calendar_Day = PARSE_DATE('%d/%m/%Y','14/10/2020') 2. last_year_same_day -> Calendar_Day = DATE_ADD(PARSE_DATE('%d/%m/%Y','14/10/2020'), INTERVAL -1 YEAR) 3. last_week_same_day -> Calendar_Day = PARSE_DATE('%d/%m/%Y','14/10/2020') - 7

Above query filter works when used individually, but fails when applied together as Calendar_Day equates three conditions.

Tried Query:

select 
      Calendar_day,
      Sales,
      from `table`
      where
        Calendar_Day = DATE_ADD(PARSE_DATE('%d/%m/%Y','14/10/2020'), INTERVAL -1 YEAR)   #last_year_same_day
        and Calendar_Day = PARSE_DATE('%d/%m/%Y','14/10/2020') - 7   #last_week_same_day
        and Calendar_Day = PARSE_DATE('%d/%m/%Y','14/10/2020')   #today

Need Solution. Thanks in Advance!

  where
    (Calendar_Day = DATE_ADD(PARSE_DATE('%d/%m/%Y','14/10/2020'), INTERVAL -1 YEAR))   #last_year_same_day
    OR (Calendar_Day = PARSE_DATE('%d/%m/%Y','14/10/2020') - 7)   #last_week_same_day
    OR (Calendar_Day = PARSE_DATE('%d/%m/%Y','14/10/2020'))   #today

I might suggest:

where Calendar_Day in (date('2020-10-14'),
                       date_add(date('2020-10-14'), interval -1 week,
                       date_add(date('2020-10-14'), interval -1 year
                      )

I see no reason to parse a date from a non-standard format, if you are passing that value into a query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM