使用 pivot sql 连接 3 个表
Using pivot sql join 3 tables
大师
我有 3 个表:
- tb_siswa
- tb_spp
- tb_bulan
以上三张表的内容如下:
- tb_siswa
- tb_spp
- tb_bulan
我想像下面的视图一样显示我的 sql,使用 sql pivot
希望有人能帮助我。
您可以在相关键上加入 table,然后使用条件聚合来转换结果:
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;
大师 我有 3 个表:
- tb_siswa
- tb_spp
- tb_bulan
以上三张表的内容如下:
- tb_siswa
- tb_spp
- tb_bulan
我想像下面的视图一样显示我的 sql,使用 sql pivot
希望有人能帮助我。
您可以在相关键上加入 table,然后使用条件聚合来转换结果:
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;