SQL:
SELECT COUNT(DISTINCT person.p_id) AS numberOfPeople,
location.l_id AS location
FROM job
INNER JOIN person ON job.j_person = person.p_id
INNER JOIN (location INNER JOIN area ON location.l_area = area.a_id) ON job.j_location = location.l_id
GROUP BY area.a_name, location.l_name
Database: The 'job' table has links with 'person' (on j_person = p_id) and 'location' (on j_location = l_id)
Table: person (list of all people in the company, PK = p_id)
+------+--------+--
| p_id | p_name | etc.
+------+--------+--
| 01 | John | ...
+------+--------+--
| 02 | Suzy | ...
+------+--------+--
| 03 | Mike | ...
+------+--------+--
| 04 | Kim | ...
+------+--------+--
Table: job (list of all jobs, PK = j_id)
+------+----------+------------+--------+
| j_id | j_person | j_location | j_type |
+------+----------+------------+--------+
| AB | 02 | cityB | type2 |
+------+----------+------------+--------+
| CD | 02 | cityA | type3 |
+------+----------+------------+--------+
| EF | 01 | cityC | type2 |
+------+----------+------------+--------+
| GH | 03 | cityB | type1 |
+------+----------+------------+--------+
| IJ | 04 | cityA | type1 |
+------+----------+------------+--------+
| KL | 04 | cityA | type2 |
+------+----------+------------+--------+
Table: location (list of all locations, PK = l_id)
+-------+----------+--------+
| l_id | l_name | l_area |
+-------+----------+----
| cityA | London | ...
+-------+----------+----
| cityB | New York | ...
+-------+----------+----
| cityC | Brussels | ...
+-------+----------+----
What I need:
A list of people per city, following is the outcome with this SQL statement:
- Area 1:
- London: 2
- New York: 2
- Area 2:
- Brussels: 1
BUT...now on to my problem
The results can't show ANY duplicate numbers/people. E.g.: Suzy (p_id = 02) has a job in both London and New York, but for the numbers to be correct in the end she may only be counted in 1 of those 2 cities.
I think I am looking for some solution that could eliminate any results that already have been included/counted, so that they can't be counted again in another/the next city. When making a sum of the amount of people per city, that result HAS TO BE the same as the total amount of records in the table 'person'.
It's not a problem when e.g. Suzy wouldn't be included in let's say New York, because the locations/cities are part a of larger area. And a person will always work within only 1 area.
I've had some trouble trying to explain what I want to achieve, plus not an English native so please let me know if something isn't clear enough.
location.l_id
in a non aggregated manner. Either add it to thegroup by
-clause or do an aggregation over it. Anything else leads to an unexpected behaviour.