Skip to content

Instantly share code, notes, and snippets.

@goodpic
Last active September 11, 2018 00:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goodpic/233a07e1b05150a9b62e6d94a5840dc6 to your computer and use it in GitHub Desktop.
Save goodpic/233a07e1b05150a9b62e6d94a5840dc6 to your computer and use it in GitHub Desktop.
Hourly sales
SELECT
TIMESTAMP_TRUNC(invoiced_at, HOUR) AS timestamp
, DATETIME_TRUNC(local_invoiced_at, HOUR) AS date_hour
, FORMAT_DATETIME("%A", local_invoiced_at) AS day
, MOD(CAST(FORMAT_DATETIME("%u", local_invoiced_at) AS int64), 7) AS day_num
, FORMAT_DATETIME("%H", local_invoiced_at) AS hour
, shop_name
, COUNT(*) AS transactions
, SUM(customers) AS customers
, SUM(sales) AS sales
, SUM(tax) AS tax
, SUM(cost) AS cost
, SUM(discount) AS discount
, SUM(sales - tax) AS net
FROM
PROJECT.DATASET.transactions
GROUP BY
shop_name, timestamp, date_hour, day, day_num, hour
ORDER BY
date_hour DESC
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment