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

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

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

android - How to create dynamically Fragment pager adapter -