Skip to content

Instantly share code, notes, and snippets.

@aus10powell
Created April 19, 2023 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 aus10powell/f0313a022ecb46387a4feef2c1ba4c25 to your computer and use it in GitHub Desktop.
Save aus10powell/f0313a022ecb46387a4feef2c1ba4c25 to your computer and use it in GitHub Desktop.
Transaction SQL
SELECT
transaction_date,
AVG(transaction_amount) AS rolling_avg
FROM (
SELECT
DATE_TRUNC('day', transaction_time) AS transaction_date,
SUM(transaction_amount) AS transaction_amount
FROM
transactions
WHERE
DATE_TRUNC('day', transaction_time) BETWEEN '2021-01-29' AND '2021-01-31'
GROUP BY
DATE_TRUNC('day', transaction_time)
) AS daily_totals
GROUP BY
transaction_date
ORDER BY
transaction_date;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment