SELECT * FROM dbo.note_lunas WHERE id_note_lunas = '2016.0006515'
SELECT * FROM dbo.note_lunas WHERE id_note_lunas = '2016.0006516'

--  1046264/DN/AJK/IV/2016
--  JV 2016/04/0012236

SELECT * FROM dbo.note_lunas_peserta WHERE id_note_lunas = '2016.0006515'
SELECT * FROM dbo.note_lunas_peserta WHERE id_note_lunas = '2016.0006516'
  

SELECT 
  id_note_terbit,
  id_peserta,
  id_produk,
  jml_terbit,
  jml_lunas,
  tgl_lunas_terakhir
FROM view_note_lunas_outstanding_peserta 
WHERE 
  jml_outstanding = 0 
  AND id_note_terbit = '2016.0004904'
  --AND id_peserta = '2016.0024662'
----------------------------------------------

SELECT
  t.id_note_terbit,
  t.id_peserta,
  t.id_produk,
  t.jml_terbit,
  COALESCE(l.jml_lunas, 0) AS jml_lunas,
  l.max_tgl_lunas AS tgl_lunas_terakhir,  
  (t.jml_terbit - COALESCE(l.jml_lunas, 0)) AS jml_outstanding
FROM
  ( SELECT
      nt.id_note_terbit,
      ntp.id_peserta,
      ntp.id_produk,
      ntp.jml_terbit
    FROM 
      note_terbit nt
      INNER JOIN
      note_terbit_peserta ntp
      ON nt.id_note_terbit = ntp.id_note_terbit
    WHERE 
      (nt.status = 'I')
  ) AS t LEFT JOIN
  ( SELECT
      nl.id_note_terbit,
      nlp.id_peserta,
      MAX(nl.tgl_lunas) AS max_tgl_lunas,    
      COALESCE(SUM(nlp.jml_lunas), 0) AS jml_lunas
    FROM 
      note_lunas nl
      INNER JOIN
      note_lunas_peserta nlp
      ON nl.id_note_lunas = nlp.id_note_lunas
    WHERE 
      (nl.status <> 'C')
    GROUP BY
      nl.id_note_terbit,
      nlp.id_peserta
  ) AS l ON (t.id_note_terbit = l.id_note_terbit) AND
            (t.id_peserta = l.id_peserta)
WHERE
  t.id_peserta = '2016.0024662'
