Skip to content

Instantly share code, notes, and snippets.

@doryokujin
Created May 26, 2015 04:12
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 doryokujin/0a4baa76742e439d3558 to your computer and use it in GitHub Desktop.
Save doryokujin/0a4baa76742e439d3558 to your computer and use it in GitHub Desktop.
SELECT goods_id, sub_category, each.category, record_number, total_amount, total_sales
FROM sales_slip_10k each
JOIN
(
SELECT
category,
COUNT(1) AS record_number,
SUM(amount) total_amount,
SUM(amount * price) AS total_sales
FROM sales_slip_10k
GROUP BY category
) aggr
ON each.category = aggr.category
ORDER BY goods_id, sub_category, each.category
LIMIT 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment