[英]How to understand results of counting distinct values in group by and having clause
Database: version 8.0.26-17 https://www.percona.com/doc/percona-server/8.0/release-notes/Percona-Server-8.0.26-17.html数据库:版本8.0.26-17 https://www.percona.com/doc/percona-server/8.0/release-notes/Percona-Server-8.0.26-17.html
I have two queries that yield different results.我有两个产生不同结果的查询。 I don't understand why.
我不明白为什么。
1) 1)
select eev_company_id,
count(distinct maj.dsd_prefix) as maj_cnt,
count(distinct min.dsd_prefix) as min_cnt
from ehev_most_recent as eev
inner join ekohubschema as ehs on ehs.ehs_subcategory = eev.eev_subcategory
left join datasourcedescription as maj on maj.dsd_prefix = eev.eev_prefix and maj.dsd_type_id = 'MAJ'
left join datasourcedescription as min on min.dsd_prefix = eev.eev_prefix and min.dsd_type_id <> 'MAJ'
where ehs.ehs_category <> 'Exclusionary Factors'
group by eev.eev_company_id
having eev.eev_company_id = 'ADD53604';
result is:结果是:
+----------------+---------+---------+
| eev_company_id | maj_cnt | min_cnt |
+----------------+---------+---------+
| ADD53604 | 2 | 1 |
+----------------+---------+---------+
The second query is pretty much the same but substituted group by eev_company_id having
with AND
:第二个查询几乎相同,但
group by eev_company_id having
包含AND
:
2) 2)
select
count(distinct maj.dsd_prefix) as maj_cnt,
count(distinct min.dsd_prefix) as min_cnt
from ehev_most_recent as eev
inner join ekohubschema as ehs on ehs.ehs_subcategory = eev.eev_subcategory
left join datasourcedescription as maj on maj.dsd_prefix = eev.eev_prefix and maj.dsd_type_id = 'MAJ'
left join datasourcedescription as min on min.dsd_prefix = eev.eev_prefix and min.dsd_type_id <> 'MAJ'
where ehs.ehs_category <> 'Exclusionary Factors' AND eev.eev_company_id = 'ADD53604';
This query results in:此查询导致:
+---------+---------+
| maj_cnt | min_cnt |
+---------+---------+
| 2 | 0 |
+---------+---------+
As you can see, the min_cnt
here is 0 while for the first query it is 1. What is the reason for the difference?如您所见,这里的
min_cnt
是 0,而对于第一个查询,它是 1。造成差异的原因是什么?
If I remove ekohubschema
join I get the same results: 3)如果我删除
ekohubschema
join,我会得到相同的结果:3)
select eev_company_id,
count(distinct maj.dsd_prefix) as maj_cnt,
count(distinct min.dsd_prefix) as min_cnt
from ehev_most_recent as eev
left join datasourcedescription as maj on maj.dsd_prefix = eev.eev_prefix and maj.dsd_type_id = 'MAJ'
left join datasourcedescription as min on min.dsd_prefix = eev.eev_prefix and min.dsd_type_id <> 'MAJ'
group by eev.eev_company_id
having eev.eev_company_id = 'ADD53604';
+----------------+---------+---------+
| eev_company_id | maj_cnt | min_cnt |
+----------------+---------+---------+
| ADD53604 | 2 | 0 |
+----------------+---------+---------+
ekohubschema
table has the following columns: ehs_category
, ehs_subcategory
and ehs_long_description
, no company ID whatsoever, and yet it interferes with the result. ekohubschema
表具有以下列: ehs_category
、 ehs_subcategory
和ehs_long_description
,没有任何公司 ID,但它会干扰结果。
I don't see any minor datasources, only major.我没有看到任何次要数据源,只有主要数据源。 This is why I struggle to find out where the count 1 (for
min_cnt
) comes from.这就是为什么我努力找出计数 1(对于
min_cnt
)的来源。
Please check the output of this query:请查看此查询的output:
select
eev_company_id,
min.dsd_prefix
from ehev_most_recent as eev
left join datasourcedescription as min on min.dsd_prefix = eev.eev_prefix and min.dsd_type_id <> 'MAJ'
where eev.eev_company_id = 'ADD53604';
I think it (the output) contains at least 1 time a 1
, if not it's a bug.我认为它(输出)至少包含 1 次
1
,否则就是错误。
I did, and I think it's a bug, see: DBFIDDLE .我做到了,我认为这是一个错误,请参阅: DBFIDDLE 。 I reported it here: bug 106539
我在这里报告: bug 106539
The bug also exists in MariaDB 10.6, see: DBFIDDLE该bug也存在于MariaDB 10.6,参见: DBFIDDLE
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.