I have a table sample
like this
SAMPLE's table
+------------+------------------------+--------+------+
| 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 |
+------------+------------------------+--------+------+
i want to GROUP BY
column id
and COUNT/SUM
how many id
when some column meet some condition. For making may question more well-known, First i will make the id
into two section
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 |
+------------+------------------------+--------+------+
Take a look at the id : 01
section first. You could see for column id_laporan
in id : 01
section, it has id 3
and 8
, and beside id_laporan
, there is column id_laporan_rekomendasi
. For id_laporan : 3
it has id_laporan_rekomendasi
: 2
and for id_laporan : 8
it has id_laporan_rekomendasi
: 3
and 4
.
NOW, each row in column id_laporan_rekomendasi
, have status
( a column beside of id_laporan_rekomendasi
). In short, what i explain before is just like these:
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 |
Each id_laporan_rekomendasi
have status
. For each different id in id_laporan_rekomendasi
and on the right column, column status
, have at least one data 3
, not 2
or other numbers, it will count as 1
else, it will count as 0
so for Id_laporan:3 it count as 1
, for Id_laporan:8 it count as 0
because even **Id_laporan:8**
has two different id for column id_laporan_rekomendasi
but column status
have no data 3
then it count as 0
. For example, the table looks like this
SAMPLE
id_laporan:8
+------------+------------------------+--------+
| id_laporan | id_laporan_rekomendasi | status |
+------------+------------------------+--------+
| 8 | 3 | 2 |
| 8 | 3 | 3 |
| 8 | 4 | 3 |
Then it will count as 2
because for each id_laporan_rekomendasi
which is different id, it will count as 1
. We could assume the table meet the condition ( as i explained before, above this sample ).that means for **Id_laporan:3**
and **Id_laporan:8**
according to my explanation, they have data 1
and 0
. after i count these, i have to SUM
both of those (that 1
and 0
) data for column id:01
. These method was same with id:02
. so the expected output will looks like below.
Other scenario
So what happen if the status 3
have already in the middle of id_laporan_rekomendasi
? , the id will definitely skipped status = 3
at the middle of id_laporan_rekomendasi
. Actually, there is no a scenario where you can found two data 3
in the same id_laporan_rekomendasi
. it will looks like this
+------------+------------------------+--------+
| 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
IMPORTANT NOTE : Each different id_laporan_rekomendasi
have only one data status = 3
, so there is no scenario where status = 3
were found twice in the same id_laporan_rekomendasi
like below scenario
False scenario
+------------+------------------------+--------+
| 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 |
This is the output that i expect for my case
+------+--------------+
| id | count |
+------+--------------+
| 01 | 1 |
| 02 | 2. |
+------+--------------+
Why the result i expect is just like above tables, based from the first table, the calculation is should be like this:
+------------+------------------------+--------+------+
| 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
+------------+------------------------+--------+------+
I have tried like this query
SELECT id, count(id) from sample group by `id`
But the result will like below
+------+--------------+
| id | count |
+------+--------------+
| 01 | 7 |
| 02 | 6 |
+------+--------------+
I know that i have to use CASE
function inside count
but i dont know how to do it with my complex table.