Сумма столбца с тем же именем (значением) в PostgreSQL - PullRequest
0 голосов
/ 25 мая 2018

Я использую следующий запрос в 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;

Тем не менее, приведенный ниже результат не тот, который я получаю:

Result of my code

Вместо этого яхотел бы следующий результат:

pegawai_nama            total    kecil     besar   sedang       khusus       
DR. JOKO TRIYONO, SPM   2        189000    0       909700       0                
DR. DJOHAR ANWAR        3        567000    0       0            0             

спасибо

Ответы [ 3 ]

0 голосов
/ 25 мая 2018

Просто имейте это как вложенный запрос и сгруппируйте его снова:

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
0 голосов
/ 25 мая 2018

Спасибо за Георгий Райчев.Ваш код очень очень помогает мне.Итак, это мой новый код

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

;

, и это результат Новый результат

0 голосов
/ 25 мая 2018

Попробуйте извлечь столбец "tindakan_golongan" из GROUP BY.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...