List Next Treatment Plan Reviews

This query demonstrates the use of a subquery to create values in a column — nextreviewdate, in this case. Adjust the date range as needed.

SELECT
    (DATE(COALESCE(
        (SELECT MIN(nextreviewdate)
                 FROM sos.v_tpreviews
                 WHERE ptepisodenum = b.ptepisodenum
                 AND tpheadernum = c.tpheadernum
                 AND spv_signandfinalize = 1
                 AND rowstatus = 'O' ),c.nextreviewdate)
    )) AS nextreviewdate,
    (a.lastname+', '+a.firstname+' / '+a.id) AS "Patient",
    d.provcode AS "Primary Provider"
FROM
  sos.patients a
  JOIN sos.ptepisodes b ON a.ptnum = b.ptnum
  JOIN sos.tpheaders c ON b.ptepisodenum = c.ptepisodenum
  LEFT OUTER JOIN sos.providers d ON b.providernum = d.providernum
WHERE
   a.flag = 0
   AND b.currentflag = 1
   AND a.dischargedate IS NULL
   AND nextreviewdate BETWEEN '2009-09-01' AND '2009-09-30'
ORDER BY "NextReviewDate","Patient"

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.