Showing posts with label employees. Show all posts
Showing posts with label employees. Show all posts

Tuesday, February 14, 2012

Breakdown by category

Let's say you a 1000 records in the Employees table, who are spread over 40 different cities.
How would you get a breakdown of how many employees in each city ?

Do I have to loop with a Count(*) for each CityID, or something ?

There must be a more straightforward method.Like this:
SELECT
CityID,
COUNT(*)
FROM
Employees
GROUP BY
CityID

The key part of that is the GROUP BY. That means to collapse theresults on the CityID column, and the COUNT(*) will keep track of howmany rows were collapsed.