2017-04-09 223 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

    太好了,谢谢各位高手 –