sql server - Add a total row to a Grouped Query -
i trying add total row grouped query. query have below:
select t3.[slpname], t1.[cardcode], t1.[cardname], t1.[shiptocode], t2.[itemcode], t2.[dscription], sum(t2.[quantity]) 'total quantity', max(t2.[price]) 'line price', sum(t2.[linetotal]) 'line total', sum(t2.[grssprofit]) 'gross profit' ocrd t0 inner join oinv t1 on t0.[cardcode] = t1.[cardcode] inner join inv1 t2 on t1.[docentry] = t2.[docentry] inner join oslp t3 on t0.[slpcode] = t3.[slpcode] t3.[slpname] = 'name' , t1.[createdate] >= dateadd(day,-7, getdate()) group t3.[slpname], t1.[cardcode], t1.[cardname], t1.[shiptocode], t2.[itemcode], t2.[dscription].
what want total row sum line - in effect sum of sum.
i've tried adding total using union clause gives me error message. when use rollup function summarises each row instead of line need summary for. can help?
many thanks.
if union without group mentioning non sum columns null, shall work fine follows:
select t3.[slpname] , t1.[cardcode] , t1.[cardname] , t1.[shiptocode] , t2.[itemcode] , t2.[dscription] , sum(t2.[quantity]) 'total quantity' , max(t2.[price]) 'line price' , sum(t2.[linetotal]) 'line total' , sum(t2.[grssprofit]) 'gross profit' ocrd t0 inner join oinv t1 on t0.[cardcode] = t1.[cardcode] inner join inv1 t2 on t1.[docentry] = t2.[docentry] inner join oslp t3 on t0.[slpcode] = t3.[slpcode] t3.[slpname] = 'name' , t1.[createdate] >= dateadd(day,-7, getdate()) group t3.[slpname] , t1.[cardcode] , t1.[cardname] , t1.[shiptocode] , t2.[itemcode] , t2.[dscription] union select null [slpname] , null [cardcode] , null [cardname] , null [shiptocode] , null [itemcode] , null [dscription] , sum(t2.[quantity]) 'total quantity' , null 'line price' , sum(t2.[linetotal]) 'line total' , sum(t2.[grssprofit]) 'gross profit' ocrd t0 inner join oinv t1 on t0.[cardcode] = t1.[cardcode] inner join inv1 t2 on t1.[docentry] = t2.[docentry] inner join oslp t3 on t0.[slpcode] = t3.[slpcode] t3.[slpname] = 'name' , t1.[createdate] >= dateadd(day,-7, getdate())
Comments
Post a Comment