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 > Counting values into separat columns

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-17-09, 08:53
Rachel_B Rachel_B is offline
Registered User
 
Join Date: Sep 2009
Posts: 21
Counting values into separat columns

Perhaps this is a daft question as I am new to SQL.

Assume I have a table like this ...
company city
Microsoft New York
IBM Chicago
Red Hat Detroit
Red Hat New York
Red Hat San Francisco
NVIDIA New York
NVIDIA LA
NVIDIA LA

What I want as an output is ....

company LA Detroit Chicago
NVIDIA 2 0 0
Red Hat 0 1 0
.... etc

Maybe I'm trying to do something that I shouldn't?

The only thing I've come up with is ...
Code:
SELECT company, COUNT(city) as LA, 0 as Detroit, 0 as Chicago
FROM customers
where city = 'LA'
GROUP BY company
union all
SELECT company, 0 as LA, COUNT(city) as Detroit, 0 as Chicago
FROM customers
where city = 'Detroit'
GROUP BY company
union all
SELECT company, 0 as LA, 0 as Detroit, COUNT(city) as Chicago
FROM customers
where city = 'Chicago'
GROUP BY company
But surely there is a neater way? Especially as in my actual code I have hundred of thousands of records!
Reply With Quote
  #2 (permalink)  
Old 09-17-09, 09:25
JarlH JarlH is offline
Registered User
 
Join Date: Dec 2008
Location: At work...
Posts: 58
Something like:
Code:
SELECT company,
       SUM(CASE WHEN CITY = 'LA' THEN 1 ELSE 0 END) as LA,
       SUM(CASE WHEN CITY = 'New York' THEN 1 ELSE 0 END) as NY,
       SUM(CASE WHEN CITY = 'Detroit' THEN 1 ELSE 0 END) as Detroit,
etc
FROM customers
GROUP BY company
Reply With Quote
  #3 (permalink)  
Old 09-17-09, 09:38
Rachel_B Rachel_B is offline
Registered User
 
Join Date: Sep 2009
Posts: 21
Excellent - many thanks
Reply With Quote
  #4 (permalink)  
Old 09-17-09, 11:01
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,084
alternatively...
Code:
SELECT company
     , COUNT(CASE WHEN city = 'LA'       THEN city END) as LA
     , COUNT(CASE WHEN city = 'New York' THEN city END) as NewYork
     , COUNT(CASE WHEN city = 'Detroit'  THEN city END) as Detroit
     , ...
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 09-17-09, 11:43
Rachel_B Rachel_B is offline
Registered User
 
Join Date: Sep 2009
Posts: 21
Thanks, that does look slightly neater.

By the way, is there a difference?
Reply With Quote
  #6 (permalink)  
Old 09-17-09, 11:49
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,084
yes, there is a difference -- one uses SUM, the other uses COUNT

but the results are the same!
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 09-17-09, 12:04
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
And one is tabbed to line up and the other isn't - it's aesthically neater.
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #8 (permalink)  
Old 09-17-09, 12:24
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,084
Quote:
Originally Posted by pootle flump
And one is tabbed to line up and the other isn't
in a word, no

it's spaced, not tabbed, but yes, it's neater

and then, of course, mine uses COUNT instead of SUM, yeah?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 09-17-09, 14:04
Rachel_B Rachel_B is offline
Registered User
 
Join Date: Sep 2009
Posts: 21
Hmm thanks for those comments!

I was meaning processing wise are they different, one may take slightly longer than the other. I don't know anything about how SQL does stuff behind the scenes, hence my question. I'd assumed they take the same time but as I'm just learning, thought I'd ask for some experts insights.
Reply With Quote
Reply

Thread Tools
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