sql - how to get the summation of column in the last row -


select csm.csmcustomername, cur.curnameofcurrency,  sum(sot.sotitemtotalamount)as 'totalitemsamount', sum(sortotaltaxamountvalue) 'totaltax', sum(sortotalchargedetailsamountvalue) 'totalcharges', (sum(sortotaltaxamountvalue)+sum(sortotalchargedetailsamountvalue)+sum(sot.sotitemtotalamount)) 'netamount' dbo.salesorder sor join dbo.currency cur on sor.sorcurrencyid=cur.curid join dbo.customermaster csm on sor.sorcustomermasterid=csm.csmid join salesorderitemdetails sot on sot.sotsalesorderid=sor.sorid group csmcustomername, curnameofcurrency rollup; 

i want sum of totalitemsamount, totaltax, totalcharges , netamount in last row of respective columns. in result set, duplication of each row, correct mistake in code.


c1 c2 c3 c4 c5 c6

a usd 1 7 2 10

b usd 3 6 3 12

c usd 5 3 0 8

d usd 4 2 1 7

   13  18   6  37 

it done via grouping sets like:

declare @t table ( code char(3), int, b int )  insert  @t values  ( 'usd', 1, 2 ),         ( 'usd', 5, 1 ),         ( 'usd', 10, 7 ),         ( 'eur', 15, 13 )   select  code ,         sum(a) ,         sum(b) b    @t group grouping sets(( code ), ( )) 

output:

code      b eur     15  13 usd     16  10 null    31  23 

Comments