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!