简体   繁体   中英

SQL Count producing incorrect results

I'm trying to calculate the number, and types of workers in a particular work slot.

There are two types of worker. Senior and Junior. The code should count how many Senior and Junior employees there are in a slot separately.

I've built a relational database in MySql. The relevant tables are worker - slotwork - slot where there is a many-to-many relationship between worker and slot. Many workers can be in an individual slot.

The results from the SQL are disturbingly inconsistent. Sometimes the count of employees is completely correct. Sometimes it appears entirely incorrect.

The simplest instance of this that I can find is as follows:

The SQL for calculating the number of junior workers is

SELECT slotwork.FK_SlotNo, Count(CASE WHEN worker.junior = 1 THEN 1 END) 
AS worker_count 
FROM worker 
INNER JOIN slotwork ON worker.EmployeeID = slotwork.FK_worker GROUP BY slotwork.FK_SlotNo;

There are no junior employees currently recorded in any slot, yet this returns result that looks like

SlotID worker_count
445 0
446 0
447 0
448 0
452 1
453 0
454 0
455 0
456 0
457 0
458 0
459 0
460 1
461 0
464 0
465 0
466 0
467 0
...

Let's take 4 of those slots above. 445, 452, 460, and 463.

445 is 0 ... correct.   There are 0 junior workers, and 5 senior workers.
452 is 1 ... incorrect. There are 0 junior workers, and 4 senior workers.
460 is 1 ... incorrect. There are 0 junior workers, and 4 senior workers.
463 is 0 ... correct.   There are 0 junior workers, and 3 senior workers.

Running the code to determine number of senior workers is mostly correct (Count(CASE WHEN worker.junior = 0 THEN 1 END)), but again with noticeable discrepancies.

Let's take the same slots as before

445 is 5 ... correct.   There are 0 junior workers, and 5 senior workers.
452 is 3 ... incorrect. There are 0 junior workers, and 4 senior workers.
460 is 3 ... incorrect. There are 0 junior workers, and 4 senior workers.
463 is 3 ... correct.   There are 0 junior workers, and 3 senior workers.

What might be generating these discrepancies?

The table for workers is structured as follows

CREATE TABLE `worker` (
  `EmployeeID` int(11) NOT NULL DEFAULT '0',
  `fullname` varchar(255) DEFAULT NULL,
  `email` varchar(255) DEFAULT NULL,
  `junior` tinyint(1) DEFAULT '0',
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Where junior is a boolean

Sample data can be found at https://pastebin.pl/view/832895ab

I think the count is making the problem.

  select count(CASE WHEN 'a' = 'b' THEN 1 else 0 END) as worker_count;
  Result --> 1
  select sum(CASE WHEN 'a' = 'b' THEN 1 else 0 END) as worker_count;
  Result --> 0

Add a junior worker and a slot then try the below query

SELECT slotwork.FK_SlotNo, sum(CASE WHEN worker.junior = 1 THEN 1 else 0 END) 
AS worker_count 
FROM worker 
INNER JOIN slotwork ON worker.EmployeeID = slotwork.FK_Employee 
GROUP BY slotwork.FK_SlotNo;

The existing query will be inconsistent because of Count() function, it will count on 0s and 1s in this case. Hope this helps!

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