943,097 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 1853
  • MS SQL RSS
Jan 26th, 2010
0

Results with Subtotals and Grandtotal

Expand Post »
Hi Friends,
Can there be a query that will SELECT fields of a table and group a column (say "CustomerID") and show all its rows along with its subtotals and then its Grandtotal in the result returned?

For Example,

CustID Items Cost ExtraCharges
---------------------------------------------------------------------------------------
A1 Cofee 15.00 2.00
A1 Tea 10.00 2.00
Subtotal: 25.00 4.00
B1 Cofee 24.00 10.00
B1 Tea 29.00 11.00
Subtotal: 53.00 21.00
GrandTotal: 78.00 25.00
Similar Threads
Reputation Points: 26
Solved Threads: 0
Junior Poster in Training
RahulV is offline Offline
92 posts
since Jun 2007
Jan 26th, 2010
0
Re: Results with Subtotals and Grandtotal
This is more befitting of a report, but it's mostly doable and could be all doable if someone tweaks it a bit.

The following assumes a Sales table with CustID, Items, Cost, and ExtraCharges fields.

MS SQL Syntax (Toggle Plain Text)
  1. SELECT CustID, Items, Cost, ExtraCharges
  2. FROM Sales
  3. Union
  4. SELECT CustID, 'Subtotal' AS Items, Sum(Cost) AS Cost, Sum(ExtraCharges) AS ExtraCharges
  5. FROM Sales
  6. GROUP BY CustID
  7. Union
  8. SELECT 'ZZZZZZZZZ' AS CustID, 'GrandTotal' AS Items, Sum(Cost) AS Cost, Sum(ExtraCharges) AS ExtraCharges
  9. FROM Sales
  10. ORDER BY CustID, Cost

Results:

MS SQL Syntax (Toggle Plain Text)
  1. A1 Tea 10.00 2.00
  2. A1 Coffee 15.00 2.00
  3. A1 Subtotal 25.00 4.00
  4. B1 Coffee 24.00 10.00
  5. B1 Tea 29.00 11.00
  6. B1 Subtotal 53.00 21.00
  7. ZZZZZZZZZ GrandTotal 78.00 25.00
Sponsor
Reputation Points: 318
Solved Threads: 135
LINQ!
apegram is offline Offline
550 posts
since Jan 2010
Jan 27th, 2010
0
Re: Results with Subtotals and Grandtotal
The above query by apegram is in general good, except for sort by cost. Typically if there was credit or discount (negative cost). Also grand total shows ugly harcoded custID.
The solution is slight modification to the same, though credit goes to apegram for writing the workable query.
sql Syntax (Toggle Plain Text)
  1. SELECT CustID, Items, Cost, ExtraCharges FROM (
  2. SELECT '1' orderCol, CustID, Items, Cost, ExtraCharges FROM Sales
  3. UNION
  4. SELECT '2' orderCol, CustID, 'Subtotal' as Items, Sum(Cost) as Cost, Sum(ExtraCharges) as ExtraCharges
  5. FROM Sales
  6. GROUP BY CustID
  7. UNION
  8. SELECT '99' orderCol, '' as CustID, 'GrandTotal' as Items, Sum(Cost) as Cost, Sum(ExtraCharges) as ExtraCharges
  9. FROM Sales
  10. ) as t1
  11. ORDER BY CASE WHEN orderCol=99 THEN 1 ELSE 0 END, CustID, orderCol, items
Reputation Points: 53
Solved Threads: 20
Junior Poster in Training
padtes is offline Offline
82 posts
since Aug 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: How to specify variable with multiple values
Next Thread in MS SQL Forum Timeline: A Giant Hurdle blocks my Giant Leap!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC