PostgreSQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesPostgreSQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 6th, 2009, 04:49 PM
jperry jperry is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 5 jperry User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 57 sec
Reputation Power: 0
3 tables 2 sums query?

Hi All,

My brain is hurting..Please help!!!

I have 3 tables:

Table A
ID
Username

Table B
ParentID
SomeNumber

Table C
ParentID
SomeNumber

I need something like this (The totals of SomeNumber from both tables B and C for a given user in A):
select a.username, sum(b.SomeNumber), sum(c.SomeNumber) from A, B, C where A.ID = B.ParentID and A.ID = C.ParentID group by a.ID

Any help would be appreciated!!!! Thanks!!

Reply With Quote
  #2  
Old October 6th, 2009, 05:05 PM
MrFujin's Avatar
MrFujin MrFujin is online now
Lord of the Dance
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Oct 2003
Posts: 1,669 MrFujin User rank is General (90000 - 100000 Reputation Level)MrFujin User rank is General (90000 - 100000 Reputation Level)MrFujin User rank is General (90000 - 100000 Reputation Level)MrFujin User rank is General (90000 - 100000 Reputation Level)MrFujin User rank is General (90000 - 100000 Reputation Level)MrFujin User rank is General (90000 - 100000 Reputation Level)MrFujin User rank is General (90000 - 100000 Reputation Level)MrFujin User rank is General (90000 - 100000 Reputation Level)MrFujin User rank is General (90000 - 100000 Reputation Level)MrFujin User rank is General (90000 - 100000 Reputation Level)MrFujin User rank is General (90000 - 100000 Reputation Level)MrFujin User rank is General (90000 - 100000 Reputation Level)MrFujin User rank is General (90000 - 100000 Reputation Level)MrFujin User rank is General (90000 - 100000 Reputation Level)MrFujin User rank is General (90000 - 100000 Reputation Level)MrFujin User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 21 h 55 m 38 sec
Reputation Power: 962
do you get any errors message?
wrong result?

you have to give some more information about you problem.

what if you group by a.username instead of group by a.ID?

Reply With Quote
  #3  
Old October 6th, 2009, 06:08 PM
jperry jperry is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 5 jperry User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 57 sec
Reputation Power: 0
Quote:
Originally Posted by MrFujin
do you get any errors message?
wrong result?

you have to give some more information about you problem.

what if you group by a.username instead of group by a.ID?


When I use a.username the query executes but there are no results. I know for sure there is data present.

Reply With Quote
  #4  
Old October 6th, 2009, 06:11 PM
jperry jperry is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 5 jperry User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 57 sec
Reputation Power: 0
Quote:
Originally Posted by jperry
When I use a.username the query executes but there are no results. I know for sure there is data present.


SNAP!!! If either of the tables is empty, I get no results. I need to outer join the two child tables right?

What does that look like?

Reply With Quote
  #5  
Old October 7th, 2009, 04:19 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,984 swampBoogie User rank is First Lieutenant (10000 - 20000 Reputation Level)swampBoogie User rank is First Lieutenant (10000 - 20000 Reputation Level)swampBoogie User rank is First Lieutenant (10000 - 20000 Reputation Level)swampBoogie User rank is First Lieutenant (10000 - 20000 Reputation Level)swampBoogie User rank is First Lieutenant (10000 - 20000 Reputation Level)swampBoogie User rank is First Lieutenant (10000 - 20000 Reputation Level)swampBoogie User rank is First Lieutenant (10000 - 20000 Reputation Level)swampBoogie User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 3 Weeks 2 Days 17 h 55 m 11 sec
Reputation Power: 151
Code:
select username, 
       (select sum(someNumber)
          from b
         where b.parentid = a.id),
       (select sum(SomeNumber) 
          from c
         where c.parentid = a.id)
  from A

Reply With Quote
  #6  
Old October 7th, 2009, 09:53 AM
jperry jperry is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 5 jperry User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 57 sec
Reputation Power: 0
Quote:
Originally Posted by swampBoogie
Code:
select username, 
       (select sum(someNumber)
          from b
         where b.parentid = a.id),
       (select sum(SomeNumber) 
          from c
         where c.parentid = a.id)
  from A


Thanks guys!!

Here's what I ended up with. Any real difference? Performance?

select a.username, sum(b.SomeNumber), sum(c.SomeNumber)
from a
left outer join b on (a.id = b.parentid)
left outer join c on (a.id = c.parentid)
group by username

Reply With Quote
  #7  
Old October 7th, 2009, 10:16 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,984 swampBoogie User rank is First Lieutenant (10000 - 20000 Reputation Level)swampBoogie User rank is First Lieutenant (10000 - 20000 Reputation Level)swampBoogie User rank is First Lieutenant (10000 - 20000 Reputation Level)swampBoogie User rank is First Lieutenant (10000 - 20000 Reputation Level)swampBoogie User rank is First Lieutenant (10000 - 20000 Reputation Level)swampBoogie User rank is First Lieutenant (10000 - 20000 Reputation Level)swampBoogie User rank is First Lieutenant (10000 - 20000 Reputation Level)swampBoogie User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 3 Weeks 2 Days 17 h 55 m 11 sec
Reputation Power: 151
The queries will give different results in some cases.

You can test with this data

Code:
create table a(id int,username varchar(20));
insert into a values(1,'swampBoogie');

create table b(parentId int,somenumber int);
insert into b values(1,47);
insert into b values(1,11);

create table c(parentId int,somenumber int);
insert into c values(1,4711);

Reply With Quote
  #8  
Old October 14th, 2009, 01:26 PM
jperry jperry is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 5 jperry User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 57 sec
Reputation Power: 0
Quote:
Originally Posted by swampBoogie
The queries will give different results in some cases.

You can test with this data

Code:
create table a(id int,username varchar(20));
insert into a values(1,'swampBoogie');

create table b(parentId int,somenumber int);
insert into b values(1,47);
insert into b values(1,11);

create table c(parentId int,somenumber int);
insert into c values(1,4711);


Welp, I just got back to this and you're RIGHT! But now I have another problem....

I need want the totals by username (i.e. group by username) but I'm getting multiple records back for a given user.

I've tried a couple of different ways to group things but nothing works right.

Thoughts?

Reply With Quote
  #9  
Old October 16th, 2009, 04:03 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,984 swampBoogie User rank is First Lieutenant (10000 - 20000 Reputation Level)swampBoogie User rank is First Lieutenant (10000 - 20000 Reputation Level)swampBoogie User rank is First Lieutenant (10000 - 20000 Reputation Level)swampBoogie User rank is First Lieutenant (10000 - 20000 Reputation Level)swampBoogie User rank is First Lieutenant (10000 - 20000 Reputation Level)swampBoogie User rank is First Lieutenant (10000 - 20000 Reputation Level)swampBoogie User rank is First Lieutenant (10000 - 20000 Reputation Level)swampBoogie User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 3 Weeks 2 Days 17 h 55 m 11 sec
Reputation Power: 151
I'm not sure I understand what you mean, but this would get the total for each user

Code:
select username, 
       coalesce((select sum(someNumber)
          from b
         where b.parentid = a.id),0) +
       coalesce((select sum(SomeNumber) 
          from c
         where c.parentid = a.id),0)
  from a


If this is not what you need, give some sample data and the expected result.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesPostgreSQL Help > 3 tables 2 sums query?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek