简体   繁体   中英

I'm having trouble with COUNT() values when joining tables SQL

I have two independent tables, tbl_timesheet and tbl_absence. tbl_timesheet will have a row every day that an employee logs into a system. tbl_absence is a single row for a unique instance of absence, where the employee isn't in work. Each table looks like:

tbl_timesheet:

Staff_ID  DEPT      LOG_DATE   
001       IT        2020-09-01
002       HR        2020-09-01
003       SALES     2020-09-01
001       IT        2020-09-02
002       HR        2020-09-02
003       SALES     2020-09-02
001       IT        2020-09-03
002       HR        2020-09-03
003       SALES     2020-09-03

tbl_absence:

Staff_ID ABSENCE_DATE
001      2020-09-10
003      2020-09-15
003      2020-09-22

I want to join the two tables, where I can count the instances of absence. I've attempted to do this using the following script:

SELECT t.Staff_ID as ID, t.DEPT as Dept, COUNT(a.Staff_ID) as 'Instances'
FROM tbl_timesheet t
     JOIN tbl.absence a
          ON t.Staff_ID = a.Staff_ID
GROUP BY t.Staff_ID, t.DEPT

I'd expect the following:

ID    Dept      Instances
001   IT        1
003   SALES     2

However due to the join between the tables, I believe the Staff_ID is being duplicated because each appears multiple times in tbl_timesheet.

Any suggestions?

when you JOIN two tables before getting distinct values of Staff_Id and Dept it will multiply the counts of records. for example staff_id='003' 2 record from absence table multiply 3 records from timesheet and you will get 6 records of it.Therefore you can code as below.

SELECT 
    t.Staff_ID as ID, 
    t.DEPT as Dept, 
    -----------
    COUNT(a.Staff_ID) as Instances
    -----------
FROM tbl_absence a
JOIN (select distinct Staff_ID, DEPT FROM tbl_timesheet) t
    ON t.Staff_ID = a.Staff_ID
GROUP BY t.Staff_ID, t.DEPT

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM