2017-09-21 50 views
-3

我有這樣的查詢UNION和JOIN表從不同的SELECT到一個表中

這是我的第一個查詢。

SELECT 
      id, 
      COUNT(id) ct_id, 
      SUM(kl) sum_kl, 
      SUM(CASE WHEN tgl_kondisi="2017-09-13" THEN 1 ELSE 0 END) as tot, 
      SUM(CASE WHEN tgl_kondisi <= "2017-09-14" THEN kl ELSE 0 end) as sum_kl2 
     FROM (
      SELECT 
       id_kondisi as id, 
       tgl_kondisi, 
       nilai_potensi_kerugian AS kl 
      FROM laporan_kondisi 
      UNION 
      SELECT 
       id_sub_kondisi, 
       tgl_kondisi, 
       nilai_potensi_kerugian 
      FROM laporan_kondisi 
     ) merged_table GROUP BY id ORDER BY id; 

結果

+------+-------+----------+------+----------+ 
| id | ct_id | sum_kl | tot | sum_kl2 | 
+------+-------+----------+------+----------+ 
| 01 |  3 | 20000000 | 3 | 20000000 | 
| 0101 |  2 | 9000000 | 2 | 9000000 | 
| 0102 |  2 | 11000000 | 2 | 11000000 | 
| 02 |  1 |  0 | 0 |  0 | 
| 0201 |  1 |  0 | 0 |  0 | 
| 0202 |  1 |  0 | 0 |  0 | 
| 0203 |  1 |  0 | 0 |  0 | 
| 03 |  2 | 4000000 | 2 | 4000000 | 
| 0301 |  1 |  0 | 1 |  0 | 
| 0302 |  2 | 4000000 | 2 | 4000000 | 
| 0303 |  1 |  0 | 1 |  0 | 
+------+-------+----------+------+----------+ 

而第二個查詢

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 
     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 

結果

+------+--------------+-------------+ 
| id | count_all_09 | count_09_13 | 
+------+--------------+-------------+ 
| 01 |   0 |   1 | 
| 0101 |   0 |   1 | 
| 0102 |   0 |   0 | 
| 02 |   0 |   0 | 
| 0201 |   0 |   0 | 
| 0202 |   0 |   0 | 
| 0203 |   0 |   0 | 
| 03 |   0 |   0 | 
| 0301 |   0 |   0 | 
| 0302 |   0 |   0 | 
| 0303 |   0 |   0 | 
+------+--------------+-------------+ 

我想UNION第一和第二查詢,以該表將看起來像這樣

+------+-------+----------+------+----------+--------------+-------------+ 
| id | ct_id | sum_kl | tot | sum_kl2 | count_all_09 | count_09_13 | 
+------+-------+----------+------+----------+--------------+-------------+ 
| 01 |  3 | 20000000 | 3 | 20000000 |  0  |  1  | 
| 0101 |  2 | 9000000 | 2 | 9000000 |  0  |  1  | 
| 0102 |  2 | 11000000 | 2 | 11000000 |  0  |  0  | 
| 02 |  1 |  0 | 0 |  0 |  0  |  0  | 
| 0201 |  1 |  0 | 0 |  0 |  0  |  0  | 
| 0202 |  1 |  0 | 0 |  0 |  0  |  0  | 
| 0203 |  1 |  0 | 0 |  0 |  0  |  0  | 
| 03 |  2 | 4000000 | 2 | 4000000 |  0  |  0  | 
| 0301 |  1 |  0 | 1 |  0 |  0  |  0  | 
| 0302 |  2 | 4000000 | 2 | 4000000 |  0  |  0  | 
| 0303 |  1 |  0 | 1 |  0 |  0  |  0  | 
+------+-------+----------+------+----------+--------------+-------------+ 

我的查詢是這樣的。

SELECT 
     id, 
     COUNT(id) ct_id, 
     SUM(kl) sum_kl, 
     SUM(CASE WHEN tgl_kondisi="2017-09-13" THEN 1 ELSE 0 END) as tot, 
     SUM(CASE WHEN tgl_kondisi <= "2017-09-14" THEN kl ELSE 0 end) as sum_kl2 
    FROM (
     SELECT 
      id_kondisi as id, 
      tgl_kondisi, 
      nilai_potensi_kerugian AS kl 
     FROM laporan_kondisi 
     UNION 
     SELECT 
      id_sub_kondisi, 
      tgl_kondisi, 
      nilai_potensi_kerugian 
     FROM laporan_kondisi 
    ) merged_table 

UNION ALL 

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 
    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 

我想UNION ALL當我FROM有一個UNION,而在我的UNIONLEFT OUTER JOIN,當我嘗試此查詢我得到了這樣的錯誤下面

的使用SELECT聲明有不同的列數

我的代碼有什麼問題嗎?或者我可能使用了無法完成的查詢? 我想這不需要SQL小提琴,最有可能有經驗的人會馬上看到什麼是錯的。

從JNevill查詢,我得到了一個結果

+------+-------+----------+------+----------+----+--------------+-------------+ 
| id | ct_id | sum_kl | tot | sum_kl2 | id | count_all_09 | count_09_13 | 
+------+-------+----------+------+----------+----+--------------+-------------+ 
| 01 | 17 | 48000000 | 13 | 48000000 | 01 |   0 |   1 | 
+------+-------+----------+------+----------+----+--------------+-------------+ 
1 row in set (0.00 sec) 
+0

當你用php,javascript和jquery標記你的問題...那些代碼在哪裏? –

+0

如果其中一個數據集的記錄多於另一個,該怎麼辦?你還可以請包括你現在有的問題,你有問題嗎? – Nope

+0

@Fran我改變了我的代碼查詢請taka看看 – Gagantous

回答

1

取而代之的是Union你正在尋找一個JoinUnion將兩個查詢的結果堆疊在一起,其中一個JOIN將兩個查詢(或表或視圖)的結果並排放置JOIN ing ON一個公用密鑰。

類似下面應該爲你工作

SELECT 
    * 
FROM 
    (
     SELECT 
      id, 
      COUNT(id) ct_id, 
      SUM(kl) sum_kl, 
      SUM(CASE WHEN tgl_kondisi="2017-09-13" THEN 1 ELSE 0 END) as tot, 
      SUM(CASE WHEN tgl_kondisi <= "2017-09-14" THEN kl ELSE 0 end) as sum_kl2 
     FROM (
      SELECT 
       id_kondisi as id, 
       tgl_kondisi, 
       nilai_potensi_kerugian AS kl 
      FROM laporan_kondisi 
      UNION 
      SELECT 
       id_sub_kondisi, 
       tgl_kondisi, 
       nilai_potensi_kerugian 
      FROM laporan_kondisi 
    ) merged_table 


    ) as t1 
    JOIN 
     (
      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 
        lap oran_rekomendasi c 
       ON 
        b.id = c.id_laporan_sebab 
       LEFT OUTER JOIN 
        laporan_tindak_lanjut d 
       ON 
        c.id = d.id_laporan_rekomendasi 
       UNION 
       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 


     ) as t2 
     ON t1.id = t2.id 

退房的w3schools example對SQL連接快速的非常高的水平底漆。

+0

它只返回一行,它只是id'01' – Gagantous

+0

之所以只返回一行,是因爲它們總和整列我編輯過我的問題a位結果 – Gagantous

+0

我猜你沒有在我們加入第一個查詢的結果集中存在所有ID。而不是'JOIN'使用'LEFT OUTER JOIN'這將從第一個查詢獲取所有記錄,並從第二個查詢獲取所有匹配記錄(在'id')。 – JNevill

相關問題