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 > Database Server Software > DB2 > new to db2 sql, and need help

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-12-10, 10:35
aznddrj307 aznddrj307 is offline
Registered User
 
Join Date: Aug 2010
Posts: 2
new to db2 sql, and need help

Hello Everyone

I am working on a sql assignment, it consist of 3 tables

table A(info about everyone):
ID, LOC, CNTRY, STATUS

table B(info about every location):
LOC, DESC, CNTRY

table C(everyone who is TYPE-E):
ID, TYPE-E


what I want to do is to generate the following summery
LOC, CNTRY, COUNT(total # of people in this Location with TYPE-E), COUNT(total # of people in this Location)

so basically I want the result categorize by LOC(location name), where CNTRY = USA(that location is in USA), then get a count of how many people are in that location with TYPE-E specific, and # of people with or without TYPE-E(basically everyone in that location)


I know I have to do double left joins and use count(), GROUP BY, but since i am new to sql, I have no idea how you use all these function together

please help? thank you so much
Reply With Quote
  #2 (permalink)  
Old 08-12-10, 13:46
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
I hate doing people's homework assignments, but I am very bored today. So, how about:

Code:
select LOC
       , sum(case when type = 'type-e' then 1 else null end)
       , count(*)
from tableA A
inner join tableC C
on a.id = b.id
where A.CNTRY = USA
group by LOC
Reply With Quote
  #3 (permalink)  
Old 08-12-10, 14:16
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Consider the following notes.
1) If there was a possibility that no one was in some location, B LEFT OUTER JOIN A would be better.

2) COUNT(expression) counts only non-null values of expression.

One example:
Code:
SELECT b.loc
     , b.cntry
     , COUNT(c.id) AS "# of people with TYPE-E"
     , COUNT(a.id) AS "total # of people"
  FROM tableB b
  LEFT OUTER JOIN
       tableA a
   ON  a.loc   = b.loc
   AND a.cntry = b.cntry
  LEFT OUTER JOIN
       tableC c
   ON  c.id   = a.id
   AND c.type = 'type-e'
 WHERE b.cntry = 'USA'
 GROUP BY
       b.loc
     , b.cntry
;
Reply With Quote
  #4 (permalink)  
Old 08-12-10, 15:27
aznddrj307 aznddrj307 is offline
Registered User
 
Join Date: Aug 2010
Posts: 2
Thank you so much guys ^_^
Reply With Quote
Reply

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