'SQL Help - Totals From Monthly Sales
So I am pulling all items tagged with a certain model prefix in a mySQL database, I am grouping by the product name, I am doing a count of the quantity (one order could have multiple of the product), and I am printing the wholesale price which is a manual CASE statement setting the price per the full model name.
The table then shows: product, model, quantity, wholesale price.
What I would like to do is have a line total (quantity * wholesale price) for each product, and at the very bottom have the monthly total.
The problem is that the wholesale price isn't an official column since its an ugly case statement setting it per line.
Here is a sanitized version of my code
SELECT
products_name,
count(products_quantity) as quantity,
CASE WHEN products_model = '123-ir' THEN 8.50
WHEN products_model = '123#262' THEN 8.50
WHEN products_model = '123#262red' THEN 10.50
WHEN products_model = '123#249' THEN 8.50
WHEN products_model = '123#217' then 3.00
WHEN products_model = '123#258' then 3.50
WHEN products_model = '123#246' then 2.00
ELSE 'fix me' END AS wholesale_price,
'' as total
FROM cart.orders INNER JOIN cart.orders_products ON orders.orders_id = orders_products.orders_id
WHERE YEAR(date_purchased) = YEAR (CURDATE()) AND MONTH(date_purchased) > MONTH (CURDATE()) - 1 -- in the last month
AND cart.orders_products.products_model like '123%'
GROUP BY products_name
ORDER BY products_name
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
