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"

Intakes for Period with Primary Dx, Primary Provider, and Office Location

I need a query that can be done by date range for charge with a CPT code of 90801 and will print out the ID, Name, Primary Dx, Primary Provider, and Office location (which is taken from Box 32 in the CSU that is attached to the charge).

SELECT
  a.id AS "ID",
  (a.lastname+', '+a.firstname) AS "Name",
  e.dxcode AS "Primary Dx",
  f.provcode AS "Primary Provider",
  g.shorthand AS "Office Location"

FROM
  sos.patients a
  JOIN sos.journal b ON a.ptnum = b.ptnum
  JOIN sos.jcharges c ON b.jnum = c.jnum
  JOIN sos.ptcsu d ON c.ptcsunum = d.ptcsunum
  LEFT OUTER JOIN sos.dx e ON d.dx1 = e.dxnum
  LEFT OUTER JOIN sos.providers f ON a.providernum = f.providernum
  LEFT OUTER JOIN sos.facilities g ON d.facilitynum = g.facilitynum
  JOIN sos.services h ON c.servicenum = h.servicenum
WHERE
   h.cptcode = '90801'
   AND b.trandate BETWEEN '2008-01-01' AND '2008-12-31'
ORDER BY "Name"

Intakes for Period with Age at Intake

I would like to have a SQL command that shows the patient name, date of birth, intake date, and only patients who had the service eval which is
90801 on the CPT code. Ultimately I am trying to find out who our new patients were in 2007 and the ages.

A bit more than a basic query, the check for a 90801 (intake) service uses a “correlated subquery” in the WHERE clause of the main query. The query also uses a custom function we have added to the SOS database called AGEINYEARS, which gives us a person’s age at any point in time. We have also included an alternate approach that gives the same result without using a subquery.

SELECT DISTINCT
pt.lastname, pt.firstname, pt.id, pt.intakeDate, pt.dob,
sos.AGEINYEARS(dob,intakeDate) AS "AgeAtIntake"
FROM
sos.Patients pt
JOIN sos.journal jou ON pt.ptnum = jou.ptnum
JOIN sos.jcharges chg ON jou.jnum = chg.jnum
JOIN sos.services srv ON chg.servicenum = srv.servicenum
WHERE
pt.intakeDate BETWEEN '2007-01-01' AND '2007-12-31'
/* at least one 90801 service in pt ledger */
AND jou.trandate >= pt.intakedate
AND srv.cptcode = '90801'
ORDER BY
pt.lastname, pt.firstname

The alternate syntax, without the subquery, becomes:

SELECT DISTINCT
  pt.lastname, pt.firstname, pt.id, pt.intakeDate, pt.DOB, 
sos.AGEINYEARS(DOB,intakeDate) AS "AgeAtIntake"
 FROM
  sos.Patients pt
  JOIN sos.journal jou ON pt.ptnum = jou.ptnum
  JOIN sos.jcharges chg ON jou.jnum = chg.jnum
  JOIN sos.services srv ON chg.servicenum = srv.servicenum
 WHERE
  pt.intakeDate BETWEEN '2007-01-01' AND '2007-12-31'
  /* at least one 90801 service in pt ledger */
  AND jou.trandate >= pt.intakedate
  AND srv.cptcode = '90801'
 ORDER BY
  pt.lastname, pt.firstname

Identifying Treatment Dropouts

We know that about 25% of patients stop coming after the third session (including intake). (Another query tells us that). We are interested in surveying these patients to learn what we could have done better to have them stay in treatment (assuming that in most cases, successful psychotherapy takes more than 3 sessions).

So, I’d like to query out patients with an intake in the last 8 months, who were seen for three or less sessions and who have not had a treatment session for at least the last 60 days. One needs to keep in mind that I want to count only active treatment sessions, not finance charges, records request, no show fees or any other non-treatment session.

SELECT
  lastname, firstname, id,
  lfeedate AS "Last Service",
  priprvcode AS "primary Provider",
  (SELECT COUNT(DISTINCT a.trandate)
      FROM sos.journal a JOIN sos.jcharges b ON a.jnum = b.jnum
      WHERE a.ptnum = pt.ptnum
      AND servicenum IN
         (SELECT servicenum
         FROM sos.services
         WHERE srvcode IN ( 'A','B','C' ) ) )  
         /*replace 'A','B','C' above with the codes you want to count */
  AS "SrvDateCount"
FROM
  sos.rv_patients AS pt
WHERE
  IntakeDate BETWEEN (TODAY( ) - 240) AND TODAY()  
  /*automatically does last 8 months. Note that TODAY() returns the same value as "CURRENT DATE" */
  AND lfeedate < (CURRENT DATE - 90)
  AND SrvDateCount <= 3

	

Future Dated Transactions And Splits

Having trouble reconciling your aging reports? The likely reason is that you have charge splits or credit splits with dates in the future. Aging is based on SPLIT dates, not service dates or credit dates. On the charge side, SOS’s aging reports use the charge split date; on the credit side they use the Date Applied in the credit split.

Use the following queries to find splits and transactions with future dates:

/* --------- charges --------------*/

SELECT
  licnum AS "Dataset", (lastname + ', '+firstname+' / '+ id) AS "Patient",
  trandate AS "Service Date", provcode AS "Provider", srvcode AS "Service",
  amount AS "Fee", chgsplamt AS "Split Amt", chgspldate AS "Split Date",
  jnum AS "Journal#"
FROM
  sos.rv_charges
WHERE
  chgspldate > today() OR trandate > today()

/*--------- credits ------------*/

SELECT
  licnum AS "Dataset", fullnameid AS "Patient",
  cre_date AS "Credit Date", credtype AS "Credit Type",
  payorname AS "Payor", cre_amount AS "Total Credit",
  crsplamt AS "Split Amt", dateapplied AS "Date Applied",
  cre_jnum AS "Journal#"
FROM
  sos.rv_creditsplits
WHERE
  dateapplied > today() OR cre_date > today()