SELECT
	pa.No_Polis,
	pa.No_Proposal,
	pa.Tgl_Mulai,
	pa.Tgl_Selesai,
	prs.Nama_Perusahaan,
	CASE pa.AdMedika_Ya
	  WHEN 1 THEN 'Ya'
	  ELSE 'Tidak'
	END AS 'AdMedika'
FROM
	dbo.Polis_Asuransi AS pa INNER JOIN
	dbo.Perusahaan AS prs ON pa.No_Prs = prs.No_Prs 
WHERE
  pa.Status = 'I' -- 697
  AND COALESCE(pa.Tgl_Selesai, GETDATE())>=GETDATE()  -- 684
  --pa.No_Proposal = dbo.func_get_no_proposal_per_tanggal(pa.No_Polis, GETDATE()) -- 613
ORDER BY
  prs.Nama_Perusahaan
---------------------------------

SELECT
	*
FROM
	(
	  SELECT
	    pa.No_Polis,
	    pa.No_Proposal,
	    pa.Tgl_Mulai,
	    pa.Tgl_Selesai,
	    prs.Nama_Perusahaan,
	    pa.AdMedika_Ya
    FROM
	    dbo.Polis_Asuransi AS pa INNER JOIN
	    dbo.Perusahaan AS prs ON pa.No_Prs = prs.No_Prs 
    WHERE
      pa.Status = 'I'
	) AS a LEFT JOIN
	(
	  SELECT
	    pa.No_Polis,
	    pa.No_Proposal,
	    pa.Tgl_Mulai,
	    pa.Tgl_Selesai,
	    prs.Nama_Perusahaan,
	    pa.AdMedika_Ya
    FROM
	    dbo.Polis_Asuransi AS pa INNER JOIN
	    dbo.Perusahaan AS prs ON pa.No_Prs = prs.No_Prs 
    WHERE
      --pa.Status = 'I' -- 697
      --AND 
      pa.No_Proposal = dbo.func_get_no_proposal_per_tanggal(pa.No_Polis, GETDATE()) -- 613
	) AS b ON a.No_Proposal = b.No_Proposal