Skip to content

Instantly share code, notes, and snippets.

@davidsommer
Created December 16, 2015 15:05
Show Gist options
  • Save davidsommer/871bc8e74c3ef0779df2 to your computer and use it in GitHub Desktop.
Save davidsommer/871bc8e74c3ef0779df2 to your computer and use it in GitHub Desktop.
Get Total Amount per User
SELECT customer_id, order_cnt, totalBestellwertUser
FROM (
SELECT customer_id,
COUNT(customer_id) AS order_cnt,
SUM(totalBestellwert) AS totalBestellwertUser
FROM (SELECT so.customer_id,
YEAR(so.created_at) AS year_ordered,
SUM(base_grand_total) AS totalBestellwert,
group_concat(si.sku SEPARATOR ',') AS skus
FROM `sales_flat_order` AS so
INNER JOIN `sales_flat_order_item` AS si ON si.order_id=so.entity_id
WHERE so.customer_id IS NOT NULL
GROUP BY entity_id
) sub_query
GROUP BY customer_id
) main_sub_query
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment