如果一堆列在GROUP BY两次遇到某些条件时怎么不COUNT?

时间:2018-03-04 11:20:26

标签: mysql sql database

SQLFiddle Demo

我有一张像这样的表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 BYidCOUNT/SUM当某列满足某些条件时id多少id。为了使提问可能更为人所知,首先我将+------------+------------------------+--------+------+ | 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: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   |
+------------+------------------------+--------+------+

Id:02

id : 01

首先查看id_laporan部分。您可以在id : 01部分中看到列3,其标识为8id_laporan,而id_laporan_rekomendasi旁边有列id_laporan : 3。对于id_laporan_rekomendasi,它有2id_laporan : 8,对于id_laporan_rekomendasi,它有34id_laporan_rekomendasi

现在,status列中的每一行都有id_laporan_rekomendasi+------------+------------------------+--------+ | id_laporan | id_laporan_rekomendasi | status | +------------+------------------------+--------+ | 3 | 2 | 2 | | 3 | 2 | 2 | | 3 | 2 | 2 | | 3 | 2 | 3 | 旁边的一列)。简而言之,我之前解释的就像这些:

Id_laporan:3

+------------+------------------------+--------+
| id_laporan | id_laporan_rekomendasi | status |
+------------+------------------------+--------+
|          8 |                      3 |      2 |
|          8 |                      3 |      2 | 
|          8 |                      4 |      2 | 

id_laporan:8

id_laporan_rekomendasi

每个status都有id_laporan_rekomendasi。对于status和右列3列中的每个不同ID,至少有一个数据2,而不是1或其他数字,它将计为{{ 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的每个id_laporan_rekomendasi,它将计为1。我们可以假设该表满足条件(正如我之前解释过的,在此示例之上)。根据我的解释,对于**Id_laporan:3****Id_laporan:8**,这意味着他们有数据1和{{1 }}。在我算上这些之后,我必须0SUM的{​​{1}}和1数据0。这些方法与id:01相同。所以预期的输出将如下所示。

其他情况

那么,如果状态id:02已经位于3的中间,会发生什么? ,id肯定会在id_laporan_rekomendasi的中间跳过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,因此没有找到status = 3的情况在同一个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函数,但我不知道如何使用我的复杂表格。

1 个答案:

答案 0 :(得分:0)

我得到了答案,感谢 Gordon Linoff Paul Spiegel ,感谢DISTINCTCASE COUNT()函数,我真的很感激(y)

我知道这段代码还有一个错误,但至少它有助于我自己即兴发布这个答案

SELECT t1.id , sum(tot) from ( 
  SELECT id, id_laporan, id_laporan_rekomendasi, 
  COUNT(distinct case when status = 3 then 1 end) as tot
  FROM sample t1
GROUP by id_laporan_rekomendasi ) t1
GROUP BY t1.id

结果

| id | sum(tot) |
|----|----------|
| 01 |        1 |
| 02 |        2 |

以下是 SQLFiddle Demo