Skip to content

Instantly share code, notes, and snippets.

@garystafford
Last active December 27, 2022 15:05
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 garystafford/23c86ee4f60605843ee0699617900fbb to your computer and use it in GitHub Desktop.
Save garystafford/23c86ee4f60605843ee0699617900fbb to your computer and use it in GitHub Desktop.
-- products
SELECT
COUNT(product_id) AS product_count,
AVG(price) AS avg_price,
AVG(cogs) AS avg_cogs,
AVG(price) - AVG(cogs) AS avg_gross_profit
FROM
products;
-- purchases
SELECT
product_id,
SUMPRECISION(quantity, 10, 0) AS quantity,
SUMPRECISION(total_purchase, 10, 2) AS sales
FROM
purchases
GROUP BY
product_id
ORDER BY
sales DESC;
-- purchasesEnriched
SELECT
product_id,
product_name,
product_category,
SUMPRECISION(purchase_quantity, 10, 0) AS quantity,
SUMPRECISION(total_purchase, 10, 2) AS sales
FROM
purchasesEnriched
GROUP BY
product_id,
product_name,
product_category
ORDER BY
sales DESC;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment