我在PostgreSQL中使用以下查询:
SELECT
pegawai_nama,
COUNT(operasi_operator_dokter) AS total,
CASE tindakan_golongan WHEN 'KECIL' THEN SUM(tariftindakan_biaya_alkes)
ELSE 0
END AS KECIL,
CASE tindakan_golongan WHEN 'BESAR' THEN SUM(tariftindakan_biaya_alkes)
ELSE 0
END AS BESAR,
CASE tindakan_golongan WHEN 'KHUSUS' THEN SUM(tariftindakan_biaya_alkes
ELSE 0
END AS SEDANG
FROM
t_operasi
LEFT JOIN m_pasien On t_operasi.operasi_pasien_norm = m_pasien.pasien_norm
LEFT JOIN t_pendaftaran on t_operasi.operasi_pendaftaran_id = t_pendaftaran.pendaftaran_id
LEFT JOIN m_tindakan ON t_operasi.operasi_tindakan_id = m_tindakan.tindakan_id
LEFT JOIN m_tarif_tindakan ON m_tindakan.tindakan_id = m_tarif_tindakan.m_tindakan_id
LEFT JOIN m_pegawai ON cast(m_pegawai.pegawai_id as varchar(10)) = t_operasi.operasi_operator_dokter
LEFT JOIN t_diagnosa_pasien ON t_diagnosa_pasien.t_pendaftaran_id = t_pendaftaran.pendaftaran_id
LEFT JOIN m_icd ON m_icd.icd_id = t_diagnosa_pasien.m_icd_id
WHERE operasi_id IS NOT NULL AND tindakan_golongan IN ('KECIL', 'BESAR', 'KHUSUS', 'SEDANG', '')
GROUP BY pegawai_nama, tindakan_golongan;
但是,下面的结果不是我想要的结果:
相反,我希望得到以下结果:
pegawai_nama total kecil besar sedang khusus
DR. JOKO TRIYONO, SPM 2 189000 0 909700 0
DR. DJOHAR ANWAR 3 567000 0 0 0
谢谢
答案 0 :(得分:0)
尝试从GROUP BY中取出“tindakan_golongan”列。
答案 1 :(得分:0)
将此作为嵌套查询,并再次对其进行分组:
select pegawai_nama, sum(total) as total , sum(kecil) as kecil, sum(besar) as besar, sum(sedang) as sedang
from (
-- insert your query here
) sub
group by pegawai_nama
答案 2 :(得分:0)
感谢Georgi Raychev。你的代码非常非常帮助我。所以这是我的新代码
SELECT
sub.pegawai_nama,
sum(sub.total) as total,
sum(sub.kecil) as kecil,
sum(sub.besar) as besar,
sum(sub.sedang) as sedang,
sum(sub.khusus) as khusus
FROM (
SELECT
pegawai_nama,
COUNT(operasi_operator_dokter) AS total,
CASE tindakan_golongan WHEN 'KECIL' THEN SUM(tariftindakan_biaya_alkes +
tariftindakan_biaya_bhp +
tariftindakan_biaya_min +
tariftindakan_biaya_max +
tariftindakan_biaya_medik +
tariftindakan_biaya_pelayanan +
tariftindakan_biaya_sarana +
tariftindakan_biaya_cover +tariftindakan_biaya)
ELSE 0
END AS kecil,
CASE tindakan_golongan WHEN 'BESAR' THEN SUM(tariftindakan_biaya_alkes +
tariftindakan_biaya_bhp +
tariftindakan_biaya_min +
tariftindakan_biaya_max +
tariftindakan_biaya_medik +
tariftindakan_biaya_pelayanan +
tariftindakan_biaya_sarana +
tariftindakan_biaya_cover +tariftindakan_biaya)
ELSE 0
END AS besar,
CASE tindakan_golongan WHEN 'KHUSUS' THEN SUM(tariftindakan_biaya_alkes +
tariftindakan_biaya_bhp +
tariftindakan_biaya_min +
tariftindakan_biaya_max +
tariftindakan_biaya_medik +
tariftindakan_biaya_pelayanan +
tariftindakan_biaya_sarana +
tariftindakan_biaya_cover +tariftindakan_biaya)
ELSE 0
END AS khusus,
CASE tindakan_golongan WHEN 'SEDANG' THEN SUM(tariftindakan_biaya_alkes +
tariftindakan_biaya_bhp +
tariftindakan_biaya_min +
tariftindakan_biaya_max +
tariftindakan_biaya_medik +
tariftindakan_biaya_pelayanan +
tariftindakan_biaya_sarana +
tariftindakan_biaya_cover +tariftindakan_biaya)
ELSE 0
END AS sedang
FROM
t_operasi
LEFT JOIN m_pasien On t_operasi.operasi_pasien_norm = m_pasien.pasien_norm
LEFT JOIN t_pendaftaran on t_operasi.operasi_pendaftaran_id = t_pendaftaran.pendaftaran_id
LEFT JOIN m_tindakan ON t_operasi.operasi_tindakan_id = m_tindakan.tindakan_id
LEFT JOIN m_tarif_tindakan ON m_tindakan.tindakan_id = m_tarif_tindakan.m_tindakan_id
LEFT JOIN m_pegawai ON cast(m_pegawai.pegawai_id as varchar(10)) = t_operasi.operasi_operator_dokter
LEFT JOIN t_diagnosa_pasien ON t_diagnosa_pasien.t_pendaftaran_id = t_pendaftaran.pendaftaran_id
LEFT JOIN m_icd ON m_icd.icd_id = t_diagnosa_pasien.m_icd_id
WHERE operasi_id IS NOT NULL
GROUP BY pegawai_nama, tindakan_golongan
) AS sub
GROUP BY sub.pegawai_nama
这就是结果 New Result