If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > aggregation problem

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-12-10, 07:44
kurupt1 kurupt1 is offline
Registered User
 
Join Date: Feb 2010
Posts: 3
aggregation problem

Hi guys,

i've been playing around to find a solution and going through forums but no joy. hopefully you can help

I have a db of customer deliveries by month. the delivery table will look something like:
dmonth customerid qty
mar-10 1 1
jun-10 2 1
mar-10 1 2
sep-10 1 1

Obviously i then have another table with all the customer info, in particular name and id.

Additionally I have a table that gives me months and quarters:
dmonth quarter
mar-10 2010q1
apr-10 2010q2

etc

What i want to get at is customer orders by customer and quarter. I can get total orders by quarter by joining the monthquarter table and the delivery table, but i cant figure out how to get it by customer as well.

Hopefully i've explained it clearly, appreciate any help.

thanks
Reply With Quote
  #2 (permalink)  
Old 02-12-10, 07:47
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
If you post up what you have that would be a good start. It sounds like you are almost there but seeing the query will help.
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #3 (permalink)  
Old 02-12-10, 08:48
kurupt1 kurupt1 is offline
Registered User
 
Join Date: Feb 2010
Posts: 3
So far I have this, which gives me total quantities for each quarter. To get it by customer as well


Code:
SELECT MonthQuarter.Quarter, sum(Delivery.Qty) As QtyQuarterly 
FROM Delivery INNER JOIN MonthQuarter ON Delivery.DMonth = MonthQuarter.DMonth 
GROUP BY MonthQuarter.Quarter
My guess to get it by CustomerID as well was to add Delivery.CustomerID to the SELECT, but that didn't work
Reply With Quote
  #4 (permalink)  
Old 02-12-10, 08:53
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
Half way there - you were not wrong to add it to the SELECT clause.

Every column in the SELECT clause must also be an aggregate (such as SUM()) or it must be in the GROUP BY.
So....
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #5 (permalink)  
Old 02-12-10, 09:07
kurupt1 kurupt1 is offline
Registered User
 
Join Date: Feb 2010
Posts: 3
Thank you, who knew it was that simple!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On