MySQL - each total sales of all items -
the following mysql query getting total sales of each item purchased user id 31 in 2014.
select op.products_name, round(sum( if(month(o.date_purchased) = 1, op.final_price * op.products_quantity,0)), 2) january, round(sum( if(month(o.date_purchased) = 2, op.final_price * op.products_quantity,0)), 2) febraury, round(sum( if(month(o.date_purchased) = 3, op.final_price * op.products_quantity,0)), 2) march, round(sum( if(month(o.date_purchased) = 4, op.final_price * op.products_quantity,0)), 2) april, round(sum( if(month(o.date_purchased) = 5, op.final_price * op.products_quantity,0)), 2) may, round(sum( if(month(o.date_purchased) = 6, op.final_price * op.products_quantity,0)), 2) june, round(sum( if(month(o.date_purchased) = 7, op.final_price * op.products_quantity,0)), 2) july, round(sum( if(month(o.date_purchased) = 8, op.final_price * op.products_quantity,0)), 2) august, round(sum( if(month(o.date_purchased) = 9, op.final_price * op.products_quantity,0)), 2) september, round(sum( if(month(o.date_purchased) = 10, op.final_price * op.products_quantity,0)), 2) october, round(sum( if(month(o.date_purchased) = 11, op.final_price * op.products_quantity,0)), 2) november, round(sum( if(month(o.date_purchased) = 12, op.final_price * op.products_quantity,0)), 2) december orders_products op left join orders o on op.orders_id = o.orders_id customers_id = 31 , year(o.date_purchased) = 2014 group op.products_id order op.products_name
and there 2 tables used.
orders_products : orders_id, products_id, products_name...
orders : orders_id, customers_id, date_purchased
i thought left joining 2 tables give me full of nulls unpurchased items... gives me rows of purchased items. how can calculate each total sales of purchased / unpurchased items?
*update
first of should use left outer join
. where
clause reducing result. should remove where
clause @ or add this:
select op.products_name, round(sum( if(month(o.date_purchased) = 1, op.final_price * op.products_quantity,0)), 2) january, round(sum( if(month(o.date_purchased) = 2, op.final_price * op.products_quantity,0)), 2) febraury, round(sum( if(month(o.date_purchased) = 3, op.final_price * op.products_quantity,0)), 2) march, round(sum( if(month(o.date_purchased) = 4, op.final_price * op.products_quantity,0)), 2) april, round(sum( if(month(o.date_purchased) = 5, op.final_price * op.products_quantity,0)), 2) may, round(sum( if(month(o.date_purchased) = 6, op.final_price * op.products_quantity,0)), 2) june, round(sum( if(month(o.date_purchased) = 7, op.final_price * op.products_quantity,0)), 2) july, round(sum( if(month(o.date_purchased) = 8, op.final_price * op.products_quantity,0)), 2) august, round(sum( if(month(o.date_purchased) = 9, op.final_price * op.products_quantity,0)), 2) september, round(sum( if(month(o.date_purchased) = 10, op.final_price * op.products_quantity,0)), 2) october, round(sum( if(month(o.date_purchased) = 11, op.final_price * op.products_quantity,0)), 2) november, round(sum( if(month(o.date_purchased) = 12, op.final_price * op.products_quantity,0)), 2) december orders_products op left outer join orders o on op.orders_id = o.orders_id (customers_id = 31 , year(o.date_purchased) = 2014) or o.date_purchased null group op.products_id order op.products_name
Comments
Post a Comment