这是我的SQL查询的一部分:
COUNT(t1.id) AS all_statuses_count,
COUNT(t1.status = 'completed') AS completed_statuses_count
我想要的是计算具有所有状态的对象,并计算具有特定状态的对象,'已完成'在这种情况下的状态。我怎么能这样做?
答案 0 :(得分:1)
SELECT COUNT(t1.id) AS all_records,
SUM(CASE WHEN t1.status = 'completed' THEN 1 ELSE 0 END) AS completed_status_count
FROM t1
这样的事情应该有效。虽然对你的第一次计数有点困惑。也许你正在寻找一些基于窗口功能的东西。老实说,我说不出来。
答案 1 :(得分:0)
试试这个:
SELECT COUNT(t1.id) AS all_statuses_count, (SELECT COUNT(t1.status) FROM
tb_Name WHERE t1.status='completed') AS completed_statuses_count
FROM tb_Name ;