Sql 为什么这两个选择COUNT(*)和否定WHERE子句的总和不正确?

Sql 为什么这两个选择COUNT(*)和否定WHERE子句的总和不正确?,sql,postgresql,Sql,Postgresql,我正在从带有where子句的表中选择行,但是否定where子句会返回更多的行 给定一个有1000行的表,a选择where返回600行,该的否定版本不应该选择where返回400行吗 select count(*) from trips trips表有420444行 我正在选择周末开始的所有旅行 select count(*) from trips where service_id in (select service_id from calendar_dates where date_part

我正在从带有where子句的表中选择行,但是否定where子句会返回更多的行

给定一个有1000行的表,a
选择where
返回600行,该
的否定版本不应该选择where
返回400行吗

select count(*) from trips
trips表有
420444行

我正在选择周末开始的所有旅行

select count(*) from trips
where service_id in
(select service_id from calendar_dates
where date_part('dow', date) in (5, 6))
select count(*) from trips
where service_id in
(select service_id from calendar_dates
where date_part('dow', date) not in (5, 6))
返回
363272

运行相同的查询,但用于周末未开始的旅行

select count(*) from trips
where service_id in
(select service_id from calendar_dates
where date_part('dow', date) in (5, 6))
select count(*) from trips
where service_id in
(select service_id from calendar_dates
where date_part('dow', date) not in (5, 6))
返回
377326

363272
+
377326
=
740598
这比
420444

当where子句中存在子查询时,
count
函数的行为是否有所不同

这是在具有GTFS数据的数据库上完成的。我不知道我错过了什么。

这不是否定的

如果您的服务id与
dow=3、4、5、6
相同,则它将出现在2个计数中

正确的否定是

select count(*) from trips
where service_id NOT in
(select service_id from calendar_dates
where date_part('dow', date) in (5, 6))
或与
不存在的等效项

不被否定

如果您的服务id与
dow=3、4、5、6
相同,则它将出现在2个计数中

正确的否定是

select count(*) from trips
where service_id NOT in
(select service_id from calendar_dates
where date_part('dow', date) in (5, 6))

如果
服务id
在与日期部分(5、6、7)相关联的
日历日期中有3行,则它将由两个查询计数如果
服务id
在与日期部分(5、6、7)相关联的
日历日期中有3行,则它将由两个查询计数