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 > MySQL > Count from 2 tables

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-26-04, 00:02
annadulldull annadulldull is offline
Registered User
 
Join Date: Oct 2004
Posts: 5
Unhappy Count from 2 tables

I have created a two tables and inserted a few data into those tables... but i'm trying to list out all the pilots and the number of types of airplane each is able to fly with the pilot's name and license number. I'm not quite sure how to do it. Can someone help me out?

Table: pilot
person_nbr license_nbr last_name first_name
1001 5033 Hui A
1002 4848 Chan C
1003 3214 Smith B
1004 1423 Wilson D

Table: files
license_nbr plane_type
5033 single engine
4848 dual engine
4848 single engine
1423 single engine
1423 dual engine
4848 turbo props

I tried using
SELECT license_nbr, COUNT(DISTINCT plane_type)
FROM files
GROUP BY license_nbr
the result is what i wanted but i don't know how i can get the pilot's last name and first name in there as well.
Reply With Quote
  #2 (permalink)  
Old 10-26-04, 00:31
joeldixon66 joeldixon66 is offline
Registered User
 
Join Date: Jul 2003
Posts: 73
Do a join from the pilot to the files table:

SELECT a.licence_nbr, a.last_name, a.first_name, COUNT(DISTINCT b.plane_type)
FROM pilot a LEFT OUTER JOIN files b ON a.licence_nbr = b.licence_nbr
GROUP BY a.licence_nbr

This way you'll also get pilots that have 0 plane types
__________________
Joel Dixon
Analyst Programmer
Melbourne, Australia

Last edited by joeldixon66; 10-26-04 at 00:34.
Reply With Quote
  #3 (permalink)  
Old 10-26-04, 01:14
annadulldull annadulldull is offline
Registered User
 
Join Date: Oct 2004
Posts: 5
For some reason there is an error message that poops up saying

SELECT a.license_nbr,a.last_name,a.first_name,COUNT(DISTI NCT b.plane_type)
2 FROM pilot a, files b
3 WHERE a.license_nbr=b.license_nbr;
SELECT a.license_nbr,a.last_name,a.first_name,COUNT(DISTI NCT b.plane_type)
*
ERROR at line 1:
ORA-00937: not a single-group group function

it works without the COUNT command.. just with b.plane_type
what should i do??
Reply With Quote
  #4 (permalink)  
Old 10-26-04, 01:19
joeldixon66 joeldixon66 is offline
Registered User
 
Join Date: Jul 2003
Posts: 73
You need to group by license_nbr

(GROUP BY a.license_nbr) as the last line.
__________________
Joel Dixon
Analyst Programmer
Melbourne, Australia
Reply With Quote
  #5 (permalink)  
Old 10-26-04, 01:24
annadulldull annadulldull is offline
Registered User
 
Join Date: Oct 2004
Posts: 5
This is what I get when I use GROUP By

SELECT a.license_nbr,a.last_name,a.first_name,COUNT(DISTI NCT b.plane_type)

FROM pilot a, files b
WHERE a.license_nbr=b.license_nbr
GROUP BY a.license_nbr;
2 3 4 SELECT a.license_nbr,a.last_name,a.first_name,COUNT(DISTI NCT b.pl
ane_type)
*
ERROR at line 1:
ORA-00979: not a GROUP BY expression

Did I do something wrong?
Reply With Quote
  #6 (permalink)  
Old 10-26-04, 01:31
joeldixon66 joeldixon66 is offline
Registered User
 
Join Date: Jul 2003
Posts: 73
You're not actually putting a space in DISTINCT are you?

You've also got to group by all fields that are not part of an aggregate function:

Code:
SELECT a.license_nbr, a.last_name, a.first_name, COUNT(DISTINCT b.plane_type)
FROM pilot a, files b
WHERE a.license_nbr=b.license_nbr
GROUP BY a.license_nbr, a.last_name, a.first_name;
__________________
Joel Dixon
Analyst Programmer
Melbourne, Australia
Reply With Quote
  #7 (permalink)  
Old 10-26-04, 01:36
annadulldull annadulldull is offline
Registered User
 
Join Date: Oct 2004
Posts: 5
Thank you so much~ I finally understand what to do when joining tables. Thank you!
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