加入其他 table 并在一个列中使用 group by 计算相同的 id
Join other table and count the same id with group by in one single column
我有四个这样的table
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 |
+----+------------------------+-------------------+
我用LEFT OUTER JOIN
得到了这样的结果
查询
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
结果
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
您可以在 SQLFiddle 上看到结果 table。
我可以像我给的 fiddle 那样加入四个 table。但同时我想知道如何像下面的table一样加入和计数:
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
所以我想要实现的是,id_s_k
将加入 id_k
,然后在 count_09_14
中将计算基于 tgl_tl
循环的 ID whic 将计算循环 id_k
和 id_s_k
,其中 tgl_tl
是 2017-09-14
。与 count_09_13
相同。它将从 id_k
和 id_s_k
计算相同的 ID,其中 tgl_tl
是 2017-09-13
。我该怎么做?
感谢
终于,我找到了解决方案,如果有人有其他解决方案(同时使用查询或在应用程序代码中),我将不胜感激
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
我有四个这样的table
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 |
+----+------------------------+-------------------+
我用LEFT OUTER JOIN
得到了这样的结果
查询
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
结果
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
您可以在 SQLFiddle 上看到结果 table。 我可以像我给的 fiddle 那样加入四个 table。但同时我想知道如何像下面的table一样加入和计数:
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
所以我想要实现的是,id_s_k
将加入 id_k
,然后在 count_09_14
中将计算基于 tgl_tl
循环的 ID whic 将计算循环 id_k
和 id_s_k
,其中 tgl_tl
是 2017-09-14
。与 count_09_13
相同。它将从 id_k
和 id_s_k
计算相同的 ID,其中 tgl_tl
是 2017-09-13
。我该怎么做?
感谢
终于,我找到了解决方案,如果有人有其他解决方案(同时使用查询或在应用程序代码中),我将不胜感激
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