1

If I had a table structure like this :

id | product name | sales dollars | projection dollars | year

And had data like this :

1 | tv | 334600 | 452000 | 2015
2 | pc | 929492 | 779564 | 2015
3 | tv | 222232 | 432134 | 2014

How can I get results into single row like this :

product name | current year sales dollars | previous year sales dollars

And resulting in this :

tv | 334600 | 222232
pc | 929492 | 0
2

this is a simple case of a conditional aggregate:

select product_name, 
       sum(case when year = 2014 then sales_dollars end) as previous_year_sales,
       sum(case when year = 2015 then sales_dollars end) as this_year_sales
from the_table
group by product_name;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.