Mysql 如何用“二”来计数;拥有;sql中的情况?

Mysql 如何用“二”来计数;拥有;sql中的情况?,mysql,sql,Mysql,Sql,我需要计算有多少id\U no具有测试活动=已批准且测试状态=已完成并按工作类型(即B)过滤的所有行编号 我试过: select date, count(*) as count from (select id, id_no, date from pt_pretest pp where work_type = 'b' group by id, id_no, date having min(test_activity) = max(test_activity)

我需要计算有多少id\U no具有测试活动=已批准且测试状态=已完成并按工作类型(即B)过滤的所有行编号

我试过:

select date, count(*) as count
from (select id, id_no, date
      from pt_pretest pp where work_type = 'b'
      group by id, id_no, date 
      having min(test_activity) = max(test_activity) and min(test_activity ) = 'approved'
      and having min(test_status) = max(test_status) and min(test_status) = 'completed'
      ) as x group by date
但它没有起作用。从上述样本中,预期结果为:

id   date         count
P    2020-02-02     1
Q    2020-02-02     1
我应该如何写我的查询,这样我才能得到预期的结果


谢谢

以下返回至少有一个
id\u no
不符合条件的所有id:

select id, count(*)
from (select id, id_no
      from t
      where not (test_activity = 'approved' and test_status = 'completed')
      group by id, id_no
     ) ii
group by id;
如果要在结果集中使用零,请在子查询中使用条件聚合:

select id, count( num_bad > 0 )
from (select id, id_no,
             sum( not (test_activity = 'approved' and test_status = 'completed') ) as num_bad
      from t
      group by id, id_no
     ) ii
group by id;

您的查询中有一个
太多。移除它

select id, id_no, date, count(*)
from pt_pretest pp
where work_type = 'b'
group by id, id_no, date 
having min(test_activity) = max(test_activity) and min(test_activity ) = 'approved'
   and min(test_status) = max(test_status) and min(test_status) = 'completed'
order by id, id_no, date;

“但它不起作用”是什么意思?你有错误吗?如果是,哪条错误消息?还是你得到了意想不到的结果?如果是这样的话,它与你期望的有什么不同?你真正想计算的是什么?您说您只想计算批准/完成了多少id_no。但是你也可以根据id和日期分组。然后创建一个查询,对每个日期的id号进行计数。然后显示一个预期结果,它不仅包含日期,还包含id。这是四个不同的东西。
select id, id_no, date, count(*)
from pt_pretest pp
where work_type = 'b'
group by id, id_no, date 
having min(test_activity) = max(test_activity) and min(test_activity ) = 'approved'
   and min(test_status) = max(test_status) and min(test_status) = 'completed'
order by id, id_no, date;