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 February 23rd, 2007, 03:36 PM
Authorized User
Join Date: Oct 2006
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?

Old February 24th, 2007, 10:31 PM
Authorized User
Join Date: Oct 2006
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.

Old February 26th, 2007, 10:23 AM
Authorized User
Join Date: Oct 2006
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.


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

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