oracle - Creating a pivot/summary view using SQL Case Statement -


i attempting write sql statement using case in order make own pivot/summary view. this spreadsheet shows table i'm working (this picture of excel, had replicate false data security purposes). learn how write sql statement show distinct item number, , total qty_sold each year (2012, 2013, 2014). essentially, have 5 columns:

item, qty_sold, cy2012, cy2013, cy2014

i've tried writing things similar this:

 select distinct item,  case when to_char(date,'yyyy') = '2012' sum(qty_sold)  end cy2012,  case when to_char(date,'yyyy') = '2013' sum(qty_sold)  end cy2013,  case when to_char(date,'yyyy') = '2014' sum(qty_sold)  end cy2014  items_sold  group item, to_char(date,'yyyy') 

but can't seem work properly. appreciated.

you close -- need use conditional aggregation:

select item,     sum(case when to_char(date,'yyyy') = '2012' qty_sold end) cy2012,     sum(case when to_char(date,'yyyy') = '2013' qty_sold end) cy2013,     sum(case when to_char(date,'yyyy') = '2014' qty_sold end) cy2014 items_sold group item 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -