sql server 2008 - Add grand total to totals in SQL -
i have bellow query , works. need add line @ bottom gives grand total
select itemtype.description, sum(price * quantity)as 'total' [mietrak].[dbo].[purchaseorderreceivingline] join item on purchaseorderreceivingline.itemfk = item.itempk join itemtype on item.itemtypefk = itemtype.itemtypepk join purchaseorderreceiving on purchaseorderreceivingline.purchaseorderreceivingfk = purchaseorderreceiving.purchaseorderreceivingpk invoicedate >= dateadd(wk, datediff(wk, 0, getdate()) - 1, 0) , invoicedate < dateadd(wk, datediff(wk, 0, getdate()), 0) group itemtype.description
use cte
, union
rows grand total row.
with cte ( select itemtype.description, sum(price * quantity)as 'total' [mietrak].[dbo].[purchaseorderreceivingline] join item on purchaseorderreceivingline.itemfk = item.itempk join itemtype on item.itemtypefk = itemtype.itemtypepk join purchaseorderreceiving on purchaseorderreceivingline.purchaseorderreceivingfk = purchaseorderreceiving.purchaseorderreceivingpk invoicedate >= dateadd(wk, datediff(wk, 0, getdate()) - 1, 0) , invoicedate < dateadd(wk, datediff(wk, 0, getdate()), 0) group itemtype.description ) select description,total cte union select 'grand total',sum(total) cte
Comments
Post a Comment