  SELECT
    Amount = CASE WHEN dbo.Note_Terbit_Master.Jenis IN (21, 23, 41, 43) THEN ROUND(SUM(ISNULL(dbo.Klaim_Ref_Detail.Jml_Dibayar, 0)) - SUM(ISNULL(dbo.Klaim_Ref_Detail.Jml_Dibayar_Old, 0)), 2)
                  WHEN dbo.Note_Terbit_Master.Jenis IN (20, 22, 40, 42) THEN -1 * ROUND(SUM(ISNULL(dbo.Klaim_Ref_Detail.Jml_Ditolak, 0)) - [dbo].[func_GetUangMuka](dbo.Klaim_Ref_Detail.No_Klaim), 2)
             END,
    CASE WHEN '960' = '008' THEN  -- jika CIMB pake nama bank panjang
              dbo.func_TrimAll(ISNULL(dbo.Bank_Master.Nama_Bank, 'No.Pst:' + dbo.Klaim_Ref_Detail.No_Peserta))
         ELSE dbo.func_TrimAll(ISNULL(dbo.Bank_Master.Nama_Pendek, 'No.Pst:' + dbo.Klaim_Ref_Detail.No_Peserta))
    END,
    SUM(COALESCE(dbo.Klaim_Ref_Detail.Jml_Dibayar, 0) - COALESCE(dbo.Klaim_Ref_Detail.Jml_Dibayar_Old, 0)),
    COALESCE(dbo.Klaim_Ref_Detail.Jml_Dibayar, 0),
    COALESCE(dbo.Klaim_Ref_Detail.Jml_Dibayar_Old, 0)
  FROM
    dbo.Klaim_Ref_Detail
    INNER JOIN dbo.Polis_Asuransi ON dbo.Klaim_Ref_Detail.No_Proposal = dbo.Polis_Asuransi.No_Proposal
    INNER JOIN dbo.Perusahaan ON dbo.Polis_Asuransi.No_Prs = dbo.Perusahaan.No_Prs
    LEFT OUTER JOIN --dbo.Peserta_Rekening ON LEFT(dbo.Klaim_Ref_Detail.No_Peserta,11)+'0' = LEFT(dbo.Peserta_Rekening.No_Peserta,11)+'0' AND   -- original (with bugs)
    dbo.Peserta_Rekening ON LEFT(dbo.Klaim_Ref_Detail.No_Peserta, 11) + '0' = dbo.Peserta_Rekening.No_Peserta
                            AND  -- modified by Pribadi Pamungkas, 2014-12-24
                            dbo.Polis_Asuransi.No_Polis = dbo.Peserta_Rekening.No_Polis
    LEFT OUTER JOIN dbo.Bank_Master ON dbo.Peserta_Rekening.Kode_Bank = dbo.Bank_Master.Kode_Bank
    LEFT OUTER JOIN dbo.Note_Terbit_Master ON dbo.Klaim_Ref_Detail.No_Ref = dbo.Note_Terbit_Master.No_Ref
    LEFT OUTER JOIN dbo.Mata_Uang ON dbo.Polis_Asuransi.Mata_Uang = dbo.Mata_Uang.Kode
    LEFT OUTER JOIN dbo.Item_Santunan_Polis ON dbo.Klaim_Ref_Detail.Kode_Santunan = dbo.Item_Santunan_Polis.Kode_Santunan
                                               AND dbo.Klaim_Ref_Detail.Id_Produk = dbo.Item_Santunan_Polis.Id_produk
  WHERE
    (dbo.Klaim_Ref_Detail.No_Ref = '2017.035089')
    AND (COALESCE(dbo.Klaim_Ref_Detail.Jml_Dibayar, 0) - COALESCE(dbo.Klaim_Ref_Detail.Jml_Dibayar_Old, 0)) <> 0
  GROUP BY
    dbo.Klaim_Ref_Detail.No_Peserta,
    dbo.Mata_Uang.Deskripsi,
    dbo.Peserta_Rekening.Rek_Nama,
    dbo.Peserta_Rekening.Rek_No,
    dbo.Bank_Master.Nama_Bank,
    dbo.Bank_Master.Nama_Pendek,
    dbo.Peserta_Rekening.Bank_Cabang,
    dbo.Note_Terbit_Master.Jenis,
    dbo.Peserta_Rekening.Kode_Bank,
    dbo.Peserta_Rekening.Bank_Lokasi,
    dbo.Klaim_Ref_Detail.No_Klaim,
    dbo.Bank_Master.Sandi_BI,
    
    dbo.Klaim_Ref_Detail.Jml_Dibayar,
    dbo.Klaim_Ref_Detail.Jml_Dibayar_Old
  --HAVING
    --SUM(COALESCE(dbo.Klaim_Ref_Detail.Jml_Dibayar, 0) - COALESCE(dbo.Klaim_Ref_Detail.Jml_Dibayar_Old, 0)) <> 0
    --COALESCE(dbo.Klaim_Ref_Detail.Jml_Dibayar, 0) - COALESCE(dbo.Klaim_Ref_Detail.Jml_Dibayar_Old, 0) <> 0
  ORDER BY
    dbo.Peserta_Rekening.Rek_Nama