Thursday, November 3, 2016

one way to pivot table

trade table:

stock side amount
----------------------
`ibm b 100
`ibm s 200
`appl b 300

-- sql server syntax
select stock, sum(iif(side='b', amount, 0)) as b, sum(iif(side='s', amount, 0)) as s
 from trade
 group by stock;

stock b s
------------------
`ibm 100 200
`aapl 300 0

No comments:

Post a Comment