简体   繁体   English

SQL 计数产生不正确的结果

[英]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.我在 MySql 中构建了一个关系数据库。 The relevant tables are worker - slotwork - slot where there is a many-to-many relationship between worker and slot.相关表是worker - slotwork - slot ,其中 worker 和 slot 之间存在多对多关系。 Many workers can be in an individual slot.许多工作人员可以在一个单独的插槽中。

The results from the SQL are disturbingly inconsistent. SQL 的结果令人不安地不一致。 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计算初级工人数量的 SQL 是

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.让我们拿上面的 4 个插槽。 445, 452, 460, and 463. 445、452、460 和 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.运行代码以确定高级工人的数量大部分是正确的(Count(CASE WHEN worker.junior = 0 THEN 1 END)),但同样存在明显的差异。

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示例数据可以在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.由于 Count() 函数,现有查询将不一致,在这种情况下它将计数 0 和 1。 Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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