I'm trying to get data in BigQuery for weekends preferably Saturday to Sunday and can I add a specific time Saturday 1am to Sunday 11:59PM?
SELECT if(date(date) >= DATE_TRUNC(current_date(), WEEK(MONDAY)),"This Week","Last Week") weekPeriod,
ga_channelGrouping,
ga_sourceMedium,
SUM(ga_sessionDuration)/SUM(ga_sessions) as avg_sessionDuration
FROM database.table
WHERE date(date) >= DATE_SUB(DATE_TRUNC(current_date(), WEEK(MONDAY)), INTERVAL 1 WEEK)
group by weekPeriod, ga_channelGrouping, ga_sourceMedium
Obtain only data from weekends:
With tbl as (SELECT sample_date from unnest(generate_timestamp_array(timestamp "2022-01-01",timestamp "2022-02-01",interval 1 hour)) as sample_date)
SELECT *,FORMAT_DATE('%A',sample_date)
From tbl
WHERE (extract(DAYofWEEK FROM sample_date)=7 and time(timestamp_trunc(sample_date,second)) between (time "01:00:00") and (time "23:59:59")) # Saturday
OR (extract(DAYofWEEK FROM sample_date)=1 and time(timestamp_trunc(sample_date,second)) between (time "00:00:00") and (time "11:00:00")) # Sunday
Create a field weekend (date of Saturday). If Sunday, substract one day to obtain the Saturday of the same weekend.
With tbl as (SELECT sample_date from unnest(generate_timestamp_array(timestamp "2022-01-01",timestamp "2022-02-01",interval 1 hour)) as sample_date)
SELECT
timestamp_trunc(Case when extract(DAYofWEEK FROM sample_date)=1 then timestamp_sub(sample_date,interval 1 day)
else sample_date
end,day) as weekend,
min(sample_date),max(sample_date),
From tbl
WHERE (extract(DAYofWEEK FROM sample_date)=7 and time(timestamp_trunc(sample_date,second)) between (time "01:00:00") and (time "23:59:59")) # Saturday
OR (extract(DAYofWEEK FROM sample_date)=1 and time(timestamp_trunc(sample_date,second)) between (time "00:00:00") and (time "11:00:00")) # Sunday
group by 1
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.