Skip to content

Instantly share code, notes, and snippets.

@dbergqvist
Created November 25, 2016 05:34
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 dbergqvist/91db91f1ae0754967661d98a3158d65c to your computer and use it in GitHub Desktop.
Save dbergqvist/91db91f1ae0754967661d98a3158d65c to your computer and use it in GitHub Desktop.
SELECT
AVG(s.totalSale)
OVER (ORDER BY orderDay
ROWS BETWEEN 7 PRECEDING AND CURRENT ROW) as runningAverage,
p.name as productName,
s.orderDay as orderDay,
FROM (
SELECT SUM(salesOrderLines.totalSale) as totalSale,
DATE(orderDate) as orderDay, salesOrderLines.productKey as productKey
FROM [google.com:bigquery-petabyte:retail_petabyte.sales_partitioned]
WHERE _PARTITIONTIME
BETWEEN TIMESTAMP("2011-01-01") AND TIMESTAMP("2011-01-31")
and REGEXP_MATCH(salesOrderLines.productKey, r'^1104537600000\-1[0-9]{2}$')
GROUP BY orderDay, productKey
) s
JOIN [google.com:bigquery-petabyte:retail_petabyte.products] p
ON s.productKey = p.productKey
ORDER BY orderDay, productName
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment