Mysql 如何在SQL中显示所有已批准状态的总数?
我有这些数据,每个系统都有很多行。它还包括类型、状态和日期。 状态为已完成、未完成、正在进行、尚未启动。 我需要根据系统和日期计算有多少行为“B”类型且状态均已批准的系统 我试过:Mysql 如何在SQL中显示所有已批准状态的总数?,mysql,sql,count,Mysql,Sql,Count,我有这些数据,每个系统都有很多行。它还包括类型、状态和日期。 状态为已完成、未完成、正在进行、尚未启动。 我需要根据系统和日期计算有多少行为“B”类型且状态均已批准的系统 我试过: select system, date, count(line) from project_system where type = 'B' group by system, date having min(status) = max(status) and min(status) = 'APPROVED' 根据上
select system, date, count(line) from project_system
where type = 'B'
group by system, date
having min(status) = max(status) and min(status) = 'APPROVED'
根据上面的脚本,我仍然没有得到想要的结果。结果为我提供了一个系统,该系统的状态已全部批准,但有总行数。它显示如下:
system date count
1 19-20-12 4
2 19-20-12 12
3 19-20-12 6
1 19-21-12 3
2 19-21-12 1
我希望看到这样的情况:
date count
19-20-12 3
19-21-12 2
我应该如何更改我的查询
谢谢。
按系统分组,日期
表示每对系统和日期都有一行
将其用作子查询,然后在主查询中按日期分组,以按日期获取计数
SELECT date, COUNT(*) AS fount
FROM (
select system, date
from project_system
where type = 'B'
group by system, date
having min(status) = max(status) and min(status) = 'APPROVED'
) AS x
GROUP BY date
无需获取子查询中的计数。您可以通过单个聚合查询执行任何操作:
select date,
( count(distinct system) -
count(case when status <> 'APPROVED' then system end)
) as num_approved_only
from project_system
where type = 'B'
group by date ;
选择日期,
(计数(不同系统)-
计数(状态为“已批准”然后系统结束时的情况)
)仅作为num_批准的
从project\u系统
其中type='B'
按日期分组;
这样做的目的是统计不同的系统,并从中减去除“批准”状态以外的其他状态的系统的数量。欢迎使用SO。请参阅:由于您有按系统分组的
,因此日期
会为每个系统
获得单独的结果。