简体   繁体   中英

Join other table and count the same id with group by in one single column

I have four table like this

Laporan_Kondisi

+----+------------+----------------+
| id | id_kondisi | id_sub_kondisi |
+----+------------+----------------+
| 37 | 01         | 0102           |
| 38 | 03         | 0302           |
| 40 | 01         | 0101           |
| 41 | 01         | 0102           |
| 42 | 01         | 0101           |
| 43 | 03         | 0301           |
| 44 | 03         | 0303           |
| 45 | 02         | 0202           |
| 46 | 01         | 0102           |
| 47 | 03         | 0301           |
| 48 | 01         | 0101           |
| 49 | 02         | 0203           |
| 50 | 03         | 0302           |
| 51 | 02         | 0202           |
| 52 | 02         | 0201           |
| 53 | 02         | 0202           |
+----+------------+----------------+

Laporan_Sebab

+----+--------------------+
| id | id_laporan_kondisi |
+----+--------------------+
|  4 | 195                |
|  5 | 34                 |
|  6 | 195                |
|  7 | 37                 |
|  8 | 37                 |
| 10 | 38                 |
| 11 | 36                 |
| 12 | 40                 |
+----+--------------------+

Laporan_Rekomendasi

+----+------------------+
| id | id_laporan_sebab |
+----+------------------+
|  4 | 4                |
|  5 | 4                |
|  6 | 5                |
|  7 | 7                |
|  8 | 7                |
|  9 | 8                |
| 10 | 8                |
| 12 | 10               |
| 13 | 10               |
| 14 | 12               |
| 15 | 12               |
+----+------------------+

Laporan Tindak Lanjut

+----+------------------------+-------------------+
| id | id_laporan_rekomendasi | tgl_tindak_lanjut |
+----+------------------------+-------------------+
|  9 |                      4 | 2017-09-13        |
| 10 |                      5 | 2017-09-13        |
| 11 |                      5 | 2017-09-13        |
| 12 |                      6 | 2017-09-13        |
| 13 |                      7 | 2017-09-13        |
| 14 |                      8 | 2017-09-13        |
| 15 |                      7 | 2017-09-13        |
| 18 |                     12 | 2017-09-14        |
| 19 |                     13 | 2017-09-14        |
| 24 |                     15 | 2017-09-14        |
| 28 |                     15 | 2017-09-14        |
| 29 |                     14 | 2017-09-14        |
| 30 |                     14 | 2017-09-14        |
+----+------------------------+-------------------+

I use LEFT OUTER JOIN and got the result like this

Query

SELECT 
    a.id as a,  
    a.id_kondisi as id_k,
    a.id_sub_kondisi as id_s_k,
    d.tgl_tindak_lanjut as tgl_tl 
FROM `laporan_kondisi` a 
LEFT OUTER JOIN 
    laporan_sebab b 
ON 
    a.id = b.id_laporan_kondisi 
LEFT OUTER JOIN 
    laporan_rekomendasi c 
ON 
    b.id = c.id_laporan_sebab 
LEFT OUTER join 
    laporan_tindak_lanjut d 
ON 
    c.id = d.id_laporan_rekomendasi ORDER by a.id_kondisi ASC,a.id_sub_kondisi ASC

Result

a   id_k    id_s_k  tgl_tl
48    01    0101    2017-09-14
40    01    0101    2017-09-14
40    01    0101    2017-09-13
40    01    0101    2017-09-14
42    01    0101    2017-09-13
40    01    0101    2017-09-14
37    01    0102    2017-09-14
37    01    0102    2017-09-14
46    01    0102    2017-09-14
37    01    0102    2017-09-14
41    01    0102    2017-09-13
37    01    0102    2017-09-13
52    02    0201    2017-09-14
45    02    0202    2017-09-14
51    02    0202    2017-09-14
53    02    0202    2017-09-13
49    02    0203    2017-09-14
47    03    0301    2017-09-14
43    03    0301    2017-09-13
38    03    0302    2017-09-13
38    03    0302    2017-09-13
50    03    0302    2017-09-13
44    03    0303    2017-09-13

You could see result table on this SQLFiddle . I could join the four tables like on the fiddle i give. butat the same time i want to know how to join and count just like the table below:

id_k    count_09_14   count_09_13
01         8                4
0101       4                2
0102       4                2
02         4                1
0201       1                0
0202       2                1  
0203       1                0
03         1                5
0301       1                1
0302       0                3
0303       0                1

SO what i want to achieve is, id_s_k will join with id_k and after that in count_09_14 will count the id that had been looping based where tgl_tl whic will count the looping id_k and id_s_k where tgl_tl is 2017-09-14 . Same with count_09_13 .It will count the same id from id_k and id_s_k where tgl_tl is 2017-09-13 . How can i do that ?

Thanks for

Finally,I got the solution, if someone have another solution ( using both query or in application code) would be apreciate

SQLFiddle

SELECT  
   id,
    SUM(CASE WHEN tgl_tindak_lanjut="2017-09-14" THEN 1 ELSE 0 end ) as count_all_09,
   SUM( CASE WHEN tgl_tindak_lanjut="2017-09-13" THEN 1 ELSE 0 END) as count_09_13

FROM (
  SELECT 
    a.id_kondisi as id,
    d.tgl_tindak_lanjut as tgl_tindak_lanjut
  FROM 
    laporan_kondisi a
  LEFT OUTER JOIN 
    laporan_sebab b 
ON 
    a.id = b.id_laporan_kondisi 
LEFT OUTER JOIN 
    laporan_rekomendasi c 
ON 
    b.id = c.id_laporan_sebab 
LEFT OUTER join 
    laporan_tindak_lanjut d 
ON 
    c.id = d.id_laporan_rekomendasi
  UNION ALL SELECT 
    a2.id_sub_kondisi,
    d2.tgl_tindak_lanjut as tgl_tindak_lanjut
  FROM laporan_kondisi a2
  LEFT OUTER JOIN 
    laporan_sebab b2
ON 
    a2.id = b2.id_laporan_kondisi 
LEFT OUTER JOIN 
    laporan_rekomendasi c2 
ON 
    b2.id = c2.id_laporan_sebab 
LEFT OUTER join 
    laporan_tindak_lanjut d2 
ON 
    c2.id = d2.id_laporan_rekomendasi
  ) merged_table

GROUP BY id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM