My data set's fields are ts, quantity, unit_price
I first need to run sum(quanitiy * unit_price) to get my sales number
ts(time stamp) is formatted like this - 2019-01-15 14:55:00 UTC
Is this what you want?
select avg(case when datecol = ? then total end) as sales_your_date,
avg(case when datecol <> ? then total end) as sales_other
from (select date(t.ts) as dte, sum(t.quantity * t.unit_price) as total
from t
where ts >= timestamp('2018-01-01') and
ts < timestamp('2019-01-01')
group by dte
) t
where extract(dayofweek from datecol) = 6 -- Saturday
This is not much different from your previous question. The same idea works, just with aggregating the data first.
?
is for the date you care about.
Below is for BigQuery Standard SQL
#standardSQL
SELECT DATE(ts) AS sale_date, quanitiy * unit_price AS sale_total,
ROUND((SUM(quanitiy * unit_price) OVER() - quanitiy * unit_price) / (COUNT(1) OVER() - 1), 2) AS sale_rest_average
FROM `project.dataset.table`
WHERE EXTRACT(DAYOFWEEK FROM DATE(ts)) = 7
AND EXTRACT(YEAR FROM DATE(ts)) = 2018
In case if you timestamp field is of TIMESTAMP data type (vs STRING) you can use just
WHERE EXTRACT(DAYOFWEEK FROM ts) = 7
AND EXTRACT(YEAR FROM ts) = 2018
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.