<强> SQLFiddle Demo
我有一张 table sample
像这样
SAMPLE 的表格
+------------+------------------------+--------+------+
| id_laporan | id_laporan_rekomendasi | status | id |
+------------+------------------------+--------+------+
| 3 | 2 | 2 | 01 |
| 3 | 2 | 2 | 01 |
| 3 | 2 | 2 | 01 |
| 3 | 2 | 3 | 01 |
| 8 | 3 | 2 | 01 |
| 8 | 3 | 2 | 01 |
| 8 | 4 | 2 | 01 |
| 7 | 1 | 2 | 02 |
| 7 | 1 | 2 | 02 |
| 7 | 1 | 2 | 02 |
| 7 | 1 | 3 | 02 |
| 7 | 5 | 2 | 02 ||
| 7 | 5 | 3 | 02 |
+------------+------------------------+--------+------+
我想要GROUP BY
栏目id
和COUNT/SUM
有多少id
当某些列满足某些条件时。为了使可能的问题更加众所周知,首先我将制作id
分为两部分
ID:01
+------------+------------------------+--------+------+
| id_laporan | id_laporan_rekomendasi | status | id |
+------------+------------------------+--------+------+
| 3 | 2 | 2 | 01 |
| 3 | 2 | 2 | 01 |
| 3 | 2 | 2 | 01 |
| 3 | 2 | 3 | 01 |
| 8 | 3 | 2 | 01 |
| 8 | 3 | 2 | 01 |
| 8 | 4 | 2 | 01 |
ID:02
| 7 | 1 | 2 | 02 |
| 7 | 1 | 2 | 02 |
| 7 | 1 | 2 | 02 |
| 7 | 1 | 3 | 02 |
| 7 | 5 | 2 | 02 ||
| 7 | 5 | 3 | 02 |
+------------+------------------------+--------+------+
看看id : 01
第一节。您可以看到列 id_laporan
在id : 01
部分,它的 ID 为 3
和8
,以及 id_laporan
旁边,有栏id_laporan_rekomendasi
。对于 id_laporan : 3
它有id_laporan_rekomendasi
:2
和 id_laporan : 8
它有id_laporan_rekomendasi
:3
和4
。
现在,列中的每一行 id_laporan_rekomendasi
,有status
( id_laporan_rekomendasi
旁边的一列)。简而言之,我之前解释的就是这样:
Id_laporan:3
+------------+------------------------+--------+
| id_laporan | id_laporan_rekomendasi | status |
+------------+------------------------+--------+
| 3 | 2 | 2 |
| 3 | 2 | 2 |
| 3 | 2 | 2 |
| 3 | 2 | 3 |
id_laporan:8
+------------+------------------------+--------+
| id_laporan | id_laporan_rekomendasi | status |
+------------+------------------------+--------+
| 8 | 3 | 2 |
| 8 | 3 | 2 |
| 8 | 4 | 2 |
每个id_laporan_rekomendasi
有status
。对于id_laporan_rekomendasi
中的每个不同的id右列 status
列,至少有一个数据3
,不是2
或其他数字,它将计为 1
否则,它将被视为 0
因此对于 Id_laporan:3 来说,它算作 1
,对于 Id_laporan:8 来说,它算作 0
因为即使**Id_laporan:8**
列 id_laporan_rekomendasi
有两个不同的 id但栏status
没有数据 3
那么它算作 0
。例如,表格如下所示
示例
id_laporan:8
+------------+------------------------+--------+
| id_laporan | id_laporan_rekomendasi | status |
+------------+------------------------+--------+
| 8 | 3 | 2 |
| 8 | 3 | 3 |
| 8 | 4 | 3 |
那么它将算作2
因为对于每个 id_laporan_rekomendasi
这是不同的 id,它将计为 1
。我们可以假设该表满足条件(正如我之前在该示例上方所解释的那样)。这意味着 **Id_laporan:3**
和**Id_laporan:8**
根据我的解释,他们有数据1
和0
。在我数完这些之后,我必须 SUM
1
列的这两个( 0
和 id:01
)数据。这些方法与 id:02
相同。所以预期的输出如下所示。
其他场景
如果状态 3
会发生什么?已经在id_laporan_rekomendasi
中间了? ,id肯定会跳过status = 3
在 id_laporan_rekomendasi
的中间。实际上,不存在能找到两条数据3
的场景。同样id_laporan_rekomendasi
。它看起来像这样
+------------+------------------------+--------+
| id_laporan | id_laporan_rekomendasi | status |
+------------+------------------------+--------+
| 3 | 2 | 2 | // 01 : 0, because status = 2
| 3 | 2 | 3 | // 01 : 1, calculation function works because status = 3
| 3 | 2 | 2 | // 01 : 1, because status = 2
| 3 | 2 | 2 | // 01 : 1, because status = 2
重要提示:各不相同id_laporan_rekomendasi
只有一种数据状态 = 3
,因此不存在 status = 3
的情况在同一个id_laporan_rekomendasi
中被发现两次就像下面的场景
错误场景
+------------+------------------------+--------+
| id_laporan | id_laporan_rekomendasi | status |
+------------+------------------------+--------+
| 3 | 2 | 2 |
| 3 | 2 | 3 | // Data status = 3, okay, for id_laporan_rekomendasi = 2, i dont need to check for the rest of id_laporan_rekomendasi's status, good grief, i will skip to the next id_laporan_rekomendasi` maybe, i will check id_laporan_rekomendasi = 3
| 3 | 2 | 3 | // what is this... ther is no way...
| 3 | 2 | 2 |
这是我对我的案例期望的输出
+------+--------------+
| id | count |
+------+--------------+
| 01 | 1 |
| 02 | 2. |
+------+--------------+
为什么我期望的结果就像上面的表一样,根据第一个表,计算应该是这样的:
+------------+------------------------+--------+------+
| id_laporan | id_laporan_rekomendasi | status | id |
+------------+------------------------+--------+------+
| 3 | 2 | 2 | 01 | // 01 : 0, because status 2
| 3 | 2 | 2 | 01 | // 01 : 0, because status 2
| 3 | 2 | 2 | 01 | // 01 : 0, because status 2
| 3 | 2 | 2 | 01 | // 01 : 0, because status 2
| 3 | 2 | 2 | 01 | // 01 : 0, because status 2
| 3 | 2 | 3 | 01 | // 01 : 1, at this point the calculation works because status 3
| 8 | 3 | 2 | 01 | // 01 : 1, because status 2
| 8 | 3 | 2 | 01 | // 01 : 1, because status 2
| 8 | 4 | 2 | 01 | // 01 : 1, because status 2
| 7 | 1 | 2 | 02 | // 02 : 0, because status 2
| 7 | 1 | 2 | 02 | // 02 : 0, because status 2
| 7 | 1 | 2 | 02 | // 02 : 0, because status 2
| 7 | 1 | 3 | 02 | // 02 : 1, at this point the calculation works because status 3
| 7 | 5 | 2 | 02 | // 02 : 1, because status 2
| 7 | 5 | 3 | 02 | // 02 : 2, at this point the calculation works because status 3
+------------+------------------------+--------+------+
我尝试过这样的查询
SELECT id, count(id) from sample group by `id`
但是结果会像下面这样
+------+--------------+
| id | count |
+------+--------------+
| 01 | 7 |
| 02 | 6 |
+------+--------------+
我知道我必须使用CASE
里面的功能count
但我不知道如何处理我的复杂表格。
最佳答案
您需要一个条件count(distinct)
。我想这就是你想要的:
select id, count(distinct case when status = 3 then id_laporan end)
from sample
group by id;
这会计算每个 id
状态为“3”的不同 id_laporan
的数量。
编辑:
SQL 表表示无序集。您的结果取决于顺序,但您没有指定顺序的列。您必须有一个。如果需要,您可以将其添加到表中,如下所示:
alter table add t_id int auto_increment;
但最终的排序可能不是您真正想要的。
使用这样的列,您可以轻松计算每个 id
的“3”的累积数量。这是使用相关子查询的方法:
select t.*,
(select count(*)
from t t2
where t2.id = t.id and
t2.? <= t.? and status = 3
)
from t;
?
表示排序的列。如果您有一组具有排序的列,那么也可以使用它。
关于mysql - 如果一堆列满足某些条件而 GROUP BY 两次,如何不进行计数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49094808/