2017-04-09 5 views
0

主 我有3個表:使用樞軸SQL加入3個表

  1. tb_siswa
  2. tb_spp
  3. tb_bulan

這裏是上面的三個表的內容:

  1. tb_siswa

enter image description here

  • tb_spp
  • enter image description here

  • tb_bulan
  • enter image description here

    我想告訴我的SQL像下面的圖,它採用SQL樞

    enter image description here

    我希望有人能幫助我。

    回答

    0

    可以聯接相關的密鑰表,然後使用條件聚集轉動結果:

    select s.nis, 
        s.nm_lengkap, 
        sum(case when b.nm_bulan = 'Januari' then p.nominal else 0 end) as Januari, 
        sum(case when b.nm_bulan = 'Februari' then p.nominal else 0 end) as Februari, 
        sum(case when b.nm_bulan = 'Maret' then p.nominal else 0 end) as Maret, 
        ... 
    from tb_siswa s 
    join tb_spp p on s.id = p.id_siswa 
    join tb_bulan b on p.id_bulan = b.id 
    group by s.nis, 
        s.nm_lengkap; 
    
    +0

    太好了,謝謝各位高手 –