简体   繁体   中英

How to add a summary row with totals in MSSQL query?

在此处输入图片说明 在此处输入图片说明 Here's the Query

SELECT 
MAX (Supplier.SupplierName) as Supplier, 
MAX (Department.Name) as Department,
MAX (Category.Name) as Category,
MAX (ItemClass.ItemLookupCode) as Matrix,
MAX (Item.ItemLookupCode) as ItemLookupCode,
MAX (Item.Description) as Description,
SUM (TransactionEntry.Quantity) as QtySold,
MAX (Item.Cost) as Cost,
MAX (Item.Price) as Price,
MAX (TransactionEntry.Price) as SoldPrice,
SUM (TransactionEntry.Price * TransactionEntry.Quantity) as TotalSale,
MAX (Item.Quantity) as OnHand

  FROM        TransactionEntry 
  INNER JOIN  [Transaction] WITH(NOLOCK) 
              ON TransactionEntry.TransactionNumber = [Transaction].TransactionNumber AND TransactionENtry.ItemType <> 9 AND TransactionEntry.StoreID = [Transaction].StoreID 
  INNER JOIN  Batch WITH(NOLOCK) 
              ON [Transaction].BatchNumber = Batch.BatchNumber AND [Transaction].StoreID = Batch.StoreID
  LEFT JOIN   Item WITH(NOLOCK) 
              ON TransactionEntry.ItemID = Item.ID 
  LEFT JOIN   Department WITH(NOLOCK) 
              ON Item.DepartmentID = Department.ID 
  LEFT JOIN   Category WITH(NOLOCK) 
              ON Item.CategoryID = Category.ID 
  LEFT JOIN   Supplier WITH(NOLOCK) 
              ON Item.SupplierID = Supplier.ID 
  LEFT JOIN   ReasonCode AS ReasonCodeDiscount WITH(NOLOCK) 
              ON TransactionEntry.DiscountReasonCodeID = ReasonCodeDiscount.ID 
  LEFT JOIN   ReasonCode AS ReasonCodeTaxChange WITH(NOLOCK) 
              ON TransactionEntry.TaxChangeReasonCodeID = ReasonCodeTaxChange.ID
  LEFT JOIN   ReasonCode AS ReasonCodeReturn WITH(NOLOCK) 
              ON TransactionEntry.ReturnReasonCodeID = ReasonCodeReturn.ID
  LEFT JOIN   Store ON [Transaction].StoreID = Store.ID
  LEFT JOIN   ItemClassComponent WITH(NOLOCK) 
      ON Item.ID = ItemClassComponent.ItemID
  LEFT JOIN   ItemClass WITH(NOLOCK) 
      ON ItemClassComponent.ItemClassID = ItemClass.ID

WHERE    DATEDIFF(week, [Transaction].Time, GETDATE()) = 1 AND
Supplier.SupplierName = 'Name'

GROUP BY Item.ItemLookupCode

Here's the query and how do I add summary row for total numbers for some of the column? I tried several things but could not find anything...

Please help!!!!

http://i29.photobucket.com/albums/c259/xkrntamax/Capture_zps511d8kun.jpg

You are probably looking for grouping sets or with rollup :

Change the group by clause to:

GROUP BY GROUPING SETS ((Item.ItemLookupCode), ())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM