Wednesday, 11 September 2013

Making MAX IF AS work with multiple inputs

Making MAX IF AS work with multiple inputs

From a MySQL database I am seeking to create a column from a field that
meets conditions.
This works:
`DROP TABLE IF EXISTS report4;
create TABLE report4 as ( select
orders_products.orders_id,
MAX( IF(products_id = 1, final_price, "-") ) AS Membership,
MAX( IF(products_id = 12, final_price, "-" ) ) AS Donation,
MAX( IF( products_id = 16, final_price, "-" ) ) AS AHCD
from orders_products
Group by orders_id M);
select * from report4;`
But I also have other product_id that I wish to inject into the new
columns of Membership and Donation.
Product with IDs 2,3,4,5,6,7,8,9,10,11 are to go into Membership. Product
with IDs 13,14,15,17,18 are to go into Donation.
It is this secondary part I cannot make work. Thoughts on what I need to do.

No comments:

Post a Comment