SQL CASE statement to do multiple thens -
i trying simplify sql program creates unique variables based on case statement. so, example, have data set following:
salesperson invoice itemcount sales type joe 1111 100 500.00 internet joe 2222 50 200.00 internet joe 3333 200 750.00 store cthulhu 4444 10 50.00 internet cthulhu 5555 20 100.00 internet
and want end like:
salesperson internetitems internetsales storeitems storesales totalitems totalsales joe 150 700.00 200 750.00 350 1450.00 cthulhu 30 150.00 . . 30 150.00
so right have like:
select salesperson, case when type='internet' itemcount else . end internetitems, case when type='internet' sales else . end internetsales, ....more code.... sum(itemcount) totalitems, sum(sales) totalsales, group 1,2,3....
i'd able condense doing like:
select salesperson, case when type='internet' internetitems=itemcount , internetsales=sales else internetitems=. , internetsales=.
is there someway when creation of multiple variables involved?
also, how sum statement work? it:
calculated(sum(internetitems)) internetitems
thanks, pyll
there's no way multiple results/columns single case, it's cut&paste&modify create different columns:
select salesperson, sum(case when type='internet' itemcount else 0 end) internetitems, sum(case when type='internet' sales else 0 end) internetsales, sum(case when type='store' itemcount else 0 end) storeitems, sum(case when type='store' sales else 0 end) storesales, sum(itemcount) totalitems, sum(sales) totalsales ... group 1
Comments
Post a Comment