[Solved] count(distinct) from joined tables returns duplicate/incorrect values

EverSQL Database Performance Knowledge Base

count(distinct) from joined tables returns duplicate/incorrect values

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:

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.

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
  2. Explicitly ORDER BY After GROUP BY (modified query below): By default, the database sorts all 'GROUP BY col1, col2, ...' queries as if you specified 'ORDER BY col1, col2, ...' in the query as well. If a query includes a GROUP BY clause but you want to avoid the overhead of sorting the result, you can suppress sorting by specifying 'ORDER BY NULL'.
Optimal indexes for this query:
ALTER TABLE `location` ADD INDEX `location_idx_l_id` (`l_id`);
ALTER TABLE `person` ADD INDEX `person_idx_p_id` (`p_id`);
The optimized query:
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 
        ORDER BY
            NULL

Related Articles



* original question posted on StackOverflow here.