Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Database > SQL Language
Password Reminder
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 23rd, 2007, 03:36 PM
Authorized User
Join Date: Oct 2006
Location: , , .
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default SQL query using "Group By" - please help

Please can you tell me how to write the following SQL query.

Essentially, I have 2 tables (primary keys marked with *):
*pk   ref   number   type
1     1     67       0
2     2     82       0
3     1     13       0
4     4     98       1
5     3     102      1 
6     2     64       0

*ref  name
1     aaa
2     bbb
3     ccc
4     ddd
I want to do a "group by" query on Table A, so as to group the ref fields where type=0 and do a sum of their associated numbers. To get this this:
ref  SUM(number)   type
1    80            0
2    146           0
I can do this OK, but here is the problem: I need to ALSO have (in my returned recordset) the name of each ref from Table B.

So overall I want this returned:
name   ref  sum(number)   type
aaa    1    80            0
bbb    2    146           0
So far I have not found the right structure to the SQL query.

If I try this...
SELECT Table_B.name, Table_A.ref, SUM(number) AS total FROM Table_A, Table_B WHERE Table_A.type = 0 AND Table_A.ref = Table_B.ref GROUP BY Table_A.ref
...I get the error that Table_B.name cannot be returned in the query as it cannot be associated with the "group by" of Table_A.ref

I've tried other variations, including INNER JOIN and nested SELECTS, but haven't got it right.

Please can you help?

Reply With Quote
  #2 (permalink)  
Old February 24th, 2007, 10:31 PM
Authorized User
Join Date: Oct 2006
Location: , , .
Posts: 10
Thanks: 0
Thanked 1 Time in 1 Post

how about this:

select Table_B.name, Table_B.name, TempGroupTable.sum
(select Table_A.ref,sum(number) as sum from Table_A group by TableA.ref)
as TempGroupTable
inner join Table_B
on TempGroupTable.ref = Table_B.ref
in other words, create the grouped result set and then join it.

This method is problematic if you want to use a "with rollup" clause, but if you don't, then it should be OK.

Reply With Quote
  #3 (permalink)  
Old February 26th, 2007, 10:23 AM
Authorized User
Join Date: Oct 2006
Location: , , .
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts

Excellent - that works.

My SQL statement ended up a bit more complex than that, but the key thing is creating a tempTable and inner-joining that with the other table.


Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
One Record From Each Group - Query rstelma SQL Server 2000 7 January 3rd, 2008 12:08 AM
SQL query retrieving last record and group by snowy SQL Language 2 December 13th, 2006 01:59 PM
Group by Weeks in a query or table lryckman Access VBA 4 December 4th, 2006 08:55 AM
SQL Query - picking latest record and group by markw SQL Language 2 April 6th, 2005 03:54 AM
Query Problem group by mateenmohd SQL Server 2000 4 February 4th, 2004 02:44 AM

All times are GMT -4. The time now is 01:31 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.