Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 9th, 2004, 09:40 AM
Authorized User
 
Join Date: Aug 2003
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default detail and sub total

How can I perform sub totals on my sql result set? Here is my basic sql code.

select year, month, customer, itemgroup, amount
from orders
where year = 2004

--- result set --------

Year Month Customer ItemGroup Amount
---- ----- -------- --------- ------
2004 1 123 ABC10 10
2004 1 123 DEF20 20
2004 2 123 ABC10 15
2004 2 456 ABC10 20
2004 2 456 DEF20 10

How do I get the result to group by and total on month or customer or item group? The following is what I envision as a result set.


Year Month Customer ItemGroup Amount
---- ----- -------- --------- ------
2004 1 123 ABC10 10
2004 1 123 DEF20 20

Total Amount Month 1: 30

Year Month Customer ItemGroup Amount
---- ----- -------- --------- ------
2004 2 123 ABC10 15
2004 2 456 ABC10 20
2004 2 456 DEF20 10

Total Amount Month 2: 45

Do I need to write this in pl/sql?
Any advice is appreciated.
-Tim
 
Old June 9th, 2004, 03:23 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try this:
Code:
  SELECT Month SUM(Amount)
    FROM Orders
   WHERE Year = 2004
GROUP BY Month
This will return the total amount for each month in the yaer of 2004.

Regards
Owain Williams
 
Old June 9th, 2004, 03:49 PM
Authorized User
 
Join Date: Aug 2003
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Owain for your reply.

Yes if I use group by I will receive total for the month.
However, I want to see the detail records as well (as depicted in my original post).

Thanks,
-Tim

 
Old June 9th, 2004, 08:13 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Tim,

Code:
select year, month, customer, itemgroup, amount
from    orders
where   year = 2004
GROUP BY Month
ORDER BY Month
COMPUTE SUM(Amount) by Month
This should get you there.
Cheers!

_________________________
-Vijay G
Strive for Perfection





Similar Threads
Thread Thread Starter Forum Replies Last Post
master-detail hhpatek ADO.NET 0 April 3rd, 2008 02:53 PM
Master Detail prasanta2expert Access VBA 1 October 1st, 2007 06:37 AM
Master-Detail with Datalist comicghozt ASP.NET 1.0 and 1.1 Basics 0 June 7th, 2006 03:41 AM
master/detail beeyule Dreamweaver (all versions) 1 January 18th, 2005 02:59 AM
ADO Master/detail -Help! SaharaWizard VB Databases Basics 2 July 24th, 2004 08:56 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.