Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 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 July 13th, 2003, 07:04 PM
Authorized User
 
Join Date: Jul 2003
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default GROUP BY Avg and Sum

Just wondering what the best way is of accomplishing this. I want to get the Average by Location and join to another file and display the total (Sum) for all locations on each row.
Table1:
   SELECT TLoc TDate TValue FROM Table1
Table2:
   SELECT BLoc Avg(BValue) FROM Table2 GROUP BY BLoc
In the end what I want is the Sum(BValue) for all locations displayed on each row from Table1. Is there a function available, or do I need to setup a dummy field in both cases that allows me to group by all locations. Thanks.

 
Old July 13th, 2003, 09:19 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 215
Thanks: 0
Thanked 0 Times in 0 Posts
Default

How do the two tables relate to eachother?

regards
David Cameron
 
Old July 14th, 2003, 01:44 AM
Authorized User
 
Join Date: Jul 2003
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The only thing they have in common is location. TLoc and BLoc

 
Old July 14th, 2003, 09:20 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Your 'specification' is a bit unclear to me.
Quote:
quote:
what I want is the Sum(BValue) for all locations displayed on each row from Table1
I've read this several times and I still am not sure what you really want. I'm not sure what the relationship between the two tables has to do with all this.

Sorry. I'll just guess you want the Average BValue by location from Table2 and the sum of BValue from Table2 for all locations, and that these two values are to be returned along with the other data in each row in Table1. The two tables have some kind of relationship based on TLoc=BLoc. I have no idea what the primary keys are:
Code:
SELECT TLoc TDate TValue,
    (SELECT AVG(BValue) FROM Table2 WHERE Table2.BLoc=T1.Tloc) as LocAvg,
    (SELECT SUM(BValue) FROM Table2) as TotalAllLocs
 FROM Table1 T1;
Jeff Mason
Custom Apps, Inc.
www.custom-apps.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I in SQL group by week & sum the values (i.e. callagga SQL Language 2 May 16th, 2008 07:25 AM
sum group values based on cell criteria darkdestroyer Excel VBA 1 January 9th, 2008 09:13 PM
Sum a column at the group level? srussell705 SharePoint Development 0 October 3rd, 2007 08:40 AM
SUM and GROUP BY in vba wdepreter Excel VBA 3 February 5th, 2007 06:11 AM
Group by , Sub Group by and Sum mateenmohd SQL Server 2000 1 March 29th, 2005 09:51 AM





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