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 > Data Access, Manipulation & Batch Languages > ANSI SQL > small basic question concerning count

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-07-03, 10:36
jif26 jif26 is offline
Junior Member
 
Join Date: Jan 2003
Posts: 2
Unhappy small basic question concerning count

Hi guys, let's say I have 2 tables.

First table has only one field (UNIQUE KEY) : TOWN

Second table has 2 fields : TOWN and MALEFEMALE

MALEFEMALE can have 3 values : F (female), M (male), U (unknown)

I want to join both tables and as a result, get 4 fields :

TOWN
NUMBERFEMALES
NUMBERMALES
NUMBERUNKNOWN

How can I do that ?
Reply With Quote
  #2 (permalink)  
Old 01-07-03, 10:46
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Re: small basic question concerning count

How to do that will depend on your DBMS and version. Newer DBMS versions have a CASE statement that you can use like this:

SELECT town,
SUM( CASE WHEN malefemale='F' THEN 1 ELSE 0 END) numberfemales,
SUM( CASE WHEN malefemale='M' THEN 1 ELSE 0 END) numbermales,
SUM( CASE WHEN malefemale='U' THEN 1 ELSE 0 END) numberunknown
FROM table
GROUP BY town;

Older versions of Oracle can achieve same result using the DECODE function.
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #3 (permalink)  
Old 01-07-03, 11:00
jif26 jif26 is offline
Junior Member
 
Join Date: Jan 2003
Posts: 2
Thumbs up

Thanks, worked!
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