2

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.

3
  • Why not group by area, rather than city then? What relevance does City hold when the data count you want is so arbitrary?
    – GavinP
    Dec 1, 2015 at 15:21
  • What determines which city a person is shown as working in? Is it whichever comes first alphabetically?
    – Boneist
    Dec 1, 2015 at 15:28
  • 1
    You are selecting a non grouped attribute location.l_id in a non aggregated manner. Either add it to the group by-clause or do an aggregation over it. Anything else leads to an unexpected behaviour.
    – S. Alcic
    Dec 1, 2015 at 15:54

2 Answers 2

1

To do this, you first have to restrict the number of jobs per person to 1 before you do the grouping. Here is one way of doing that:

with person as (select 1 p_id, 'John' p_name from dual union all
                select 2 p_id, 'Suzy' p_name from dual union all
                select 3 p_id, 'Mike' p_name from dual union all
                select 4 p_id, 'Kim' p_name from dual),
       jobs as (select 'AB' j_id, 2 j_person, 'cityB' j_location, 'type2' j_type from dual union all
                select 'CD' j_id, 2 j_person, 'cityA' j_location, 'type3' j_type from dual union all
                select 'EF' j_id, 1 j_person, 'cityC' j_location, 'type2' j_type from dual union all
                select 'GH' j_id, 3 j_person, 'cityB' j_location, 'type1' j_type from dual union all
                select 'IJ' j_id, 4 j_person, 'cityA' j_location, 'type1' j_type from dual union all
                select 'KL' j_id, 4 j_person, 'cityA' j_location, 'type2' j_type from dual),
   location as (select 'cityA' l_id, 'London' l_name from dual union all
                select 'cityB' l_id, 'New York' l_name from dual union all
                select 'cityC' l_id, 'Brussels' l_name from dual)
-- end of setting up some subqueries to mimic your tables with data in them. See SQL below:
select   location_name,
         count(distinct person_id) number_of_people
from     (select p.p_id person_id,
                 p.p_name person_name,
                 l.l_name location_name,
                 j.j_type job_type,
                 row_number() over (partition by p.p_id order by j.j_type, l.l_name) rn
          from   jobs j
                 inner join person p on j.j_person = p.p_id
                 inner join location l on j.j_location = l.l_id)
where    rn = 1
group by location_name;

LOCATION_NAME NUMBER_OF_PEOPLE
------------- ----------------
London                       1
Brussels                     1
New York                     2

You can see that I've used the row_number() analytic function to assign a number to the rows per each p_id, in the order of the job type and location name. If the logic to decide which location to list against the row with row_number = 1 is different to that, you'll need to amend the ordering clause appropriately.

From there, it's just a matter of filtering the results to only display the first row for each p_id and then grouping the results to get the distinct number of people.

1

Oh the joys of reporting - to have numbers that aren't quite right at each city to have them line up in a total that represents our count of employees? Or to have the cities right but then totalling them comes up with a number larger than our payroll? Because really, in this case the line items and totals DO count different things because "people who do work at this office" is not the same as "people who do work in the company"

One other option - fractional people!

If a person works in two cities, Show them both under a "count of people working here", but also sum a modifier to subtract from the total count to get your total number of employees.

e.g.)

with person as (select 1 p_id, 'John' p_name from dual union all
                select 2 p_id, 'Suzy' p_name from dual union all
                select 3 p_id, 'Mike' p_name from dual union all
                select 4 p_id, 'Kim' p_name from dual),
       jobs as (select 'AB' j_id, 2 j_person, 'cityB' j_location, 'type2' j_type from dual union all
                select 'CD' j_id, 2 j_person, 'cityA' j_location, 'type3' j_type from dual union all
                select 'EF' j_id, 1 j_person, 'cityC' j_location, 'type2' j_type from dual union all
                select 'GH' j_id, 3 j_person, 'cityB' j_location, 'type1' j_type from dual union all
                select 'IJ' j_id, 4 j_person, 'cityA' j_location, 'type1' j_type from dual union all
                select 'KL' j_id, 4 j_person, 'cityA' j_location, 'type2' j_type from dual),
     lctn   as (select 'cityA' l_id, 'London' l_name from dual union all
                select 'cityB' l_id, 'New York' l_name from dual union all
                select 'cityC' l_id, 'Brussels' l_name from dual)
-- end of setting up some subqueries to mimic your tables with data in them. See SQL below:
select   location_name,
         location_jobs             number_of_distinct_jobs,
         count(distinct person_id) cnt_of_people_working_here,
         sum(distinct case when person_jobs = 1 then 0 else (1-person_jobs) end) shared_people
  FROM(  select p.p_id person_id,
                 l.l_name location_name,
                 1/(count(distinct l_name) over (partition by p.p_id)) person_jobs, 
                 count(distinct j_id)   over (partition by l_name) location_jobs 
          from   jobs j
                 inner join person p on j.j_person = p.p_id
                 inner join lctn l on j.j_location = l.l_id)
group by location_name, location_jobs;                 



LOCATION_NAME   NUMBER_OF_DISTINCT_JOBS   CNT_OF_PEOPLE_WORKING_HERE  SHARED_PEOPLE                          
"London"        3                         2                           0.5                                    
"Brussels"      1                         1                           0                                      
"New York"      2                         2                           0.5                                    

When it comes to your total line, if you sum up the count_of_people_working_here and subtract the sum of shared_people, you get your total payroll. Anything else and either your lines or your totals are off because, as stated, you are grouping at different levels.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.