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

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -