sql server - SQL: Trying to understand IF/ELSE -
select case r.sourceid when '1' 'itunes' when '2' 'sfr' when '3' 'orange' else 'others' end source , cast(sum (r.salesvolume) decimal(14, 4) ) volume , cast(sum (r.salesvolume * r.customerprice) decimal(14, 4) ) value rawdata r inner join product p on p.productid = r.productid inner join calendar c on r.dayid = c.dayid c.weekid between (20145227) , (20155230) , p.contentflavor in ('sd', 'hd') , p.vodest in ('vod','est') , p.distributor in ('m6snd') group case r.sourceid when '1' 'itunes' when '2' 'sfr' when '3' 'orange' else 'others' end
the result of above query is:
source volume value itunes 48316.0000 506067.2600
this result ok since source table rawdata
doesnt contain values sourceid
2
or 3
. want result is:
source volume value itunes 48316.0000 506067.2600 sfr 0 0 orange 0 0 others 0 0
if there no value corresponding column parameter need 0 assume done using if/else not sure how?
with of cte way it. (replace first query more dynamic if want)
with mychoices (choices) ( select choices ( values ('itunes'), ('sfr'), ('orange'), ('others') ) [ ] (choices) ), myquery ([source],[volume],[value]) ( select case r.sourceid when '1' 'itunes' when '2' 'sfr' when '3' 'orange' else 'others' end source , cast(sum (r.salesvolume) decimal(14, 4) ) volume , cast(sum (r.salesvolume * r.customerprice) decimal(14, 4) ) value rawdata r inner join product p on p.productid = r.productid inner join calendar c on r.dayid = c.dayid c.weekid between (20145227) , (20155230) , p.contentflavor in ('sd', 'hd') , p.vodest in ('vod','est') , p.distributor in ('m6snd') group case r.sourceid when '1' 'itunes' when '2' 'sfr' when '3' 'orange' else 'others' end ) select c.choices, isnull(q.volume,0)volume, isnull(q.value,0)value mychoices c left join myquery q on c.choices = q.[source]
Comments
Post a Comment