bryan
bryan

Reputation: 305

MySQL Query assistance apply filtering to two columns

Hello I am not sure how to do this query looking for some guidance...

The output should be a column with a single row 'Total Sales for 2004' and
another column with a single row 'Total Sales for 2005'.

using classicmodels.orders (orderNumber, OrderDate) and classicmodels.orderdetails(orderNumber,PriceEach)

How would you be able to apply filtering clauses to two different columns?

Thanks in advance

Upvotes: 1

Views: 49

Answers (1)

GMB
GMB

Reputation: 222672

You seem to be looking for conditional aggregation :

SELECT
    SUM(CASE WHEN YEAR(o.OrderDate) = 2004 THEN od.PriceEach ELSE 0 END) Total_Sales_for_2004,
    SUM(CASE WHEN YEAR(o.OrderDate) = 2005 THEN od.PriceEach ELSE 0 END) Total_Sales_for_2005
FROM 
    orders o
    INNER JOIN orderdetails od ON od.orderNumber = o.orderNumber
WHERE YEAR(o.OrderDate) IN (2004, 20O5)

Upvotes: 2

Related Questions