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

	

Fees and Service Count by Payor, CPT, Service, SortCode, Month, and Year

I have a query to tell me the sum of fees and number of services by payor, cpt code, service code, and SortCode, but I would like to break down that data by month and year as well. Can that be done?

This query illustrates the use of a subquery as a virtual table. That is, we use an embedded SELECT statement in the main query’s FROM clause to create a result set that we then treat as if it were a table, JOINing it to other tables to give us the results that we want. To get the month and year grouping,
add YEAR(trandate), MONTH(trandate) to the SELECT list, the GROUP BY, and the ORDER BY.

Note that the column order in the SELECT clause makes no difference, so you can shuffle them around as you like, but be sure to separate columns with commas, except no comma after the last one.

Assuming you would still want the sortcode and payor to be higher order grouping, the query would become:

SELECT
  YEAR(a.trandate) AS "Year",
  MONTH(a.trandate) AS "Month",
  COALESCE(d.payorname,'ALL PAYORS') AS payorname,
  COALESCE(b.cptcode,'ALL CPTS') AS cptcode,
  COALESCE(a.srvcode,'ALL SRVCODES') AS srvcode,
  a.sortcode AS sortcode,
  COUNT(*) AS "N",
  SUM(a.amount) AS "SumFees"
FROM
  (SELECT DISTINCT jnum,srvcode,servicenum,ptpayornum,trandate,amount,sortcode FROM sos.rv_charges) a
  JOIN sos.services b ON a.servicenum = b.servicenum
  JOIN sos.ptpayors c ON a.ptpayornum = c.ptpayornum
  JOIN sos.payors d ON c.payornum = d.payornum
WHERE
  d.payortype = 'I'
  AND trandate BETWEEN '2008-4-01' AND '2008-9-11'
GROUP BY
  ROLLUP(a.sortcode,d.payorname,"Year","Month", b.cptcode, a.srvcode)
ORDER BY
  sortcode,payorname,"Year","Month",cptcode,srvcode

E-Mail Addresses For Active Patients

I need a list of active patients with their email addresses. Only list those with emails, and sort in order by name.

SELECT DISTINCT
a.lastname, a.firstname, b.email
FROM
sos.patients a JOIN payors b ON a.payornum = b.payornum
WHERE
flag = 0 AND dischargedate IS NULL AND email > ''
ORDER BY
a.lastname, a.firstname, b.email

ICD-9 Diagosis Codes In Use and ICD-10 Suggestions from CMS GEMs

This query returns a list of diagnoses that are currently in use in Claim Setups in OM and in DSM Profiles in CM. These are the ones that you will have to replace by the new ICD-10 diagnosis codes on or before October of 2014.

The query itself is a main query that lists diagnoses that appear in any of a bunch of fields in OM and CM and a subquery used in the WHERE clause. The subquery contains a simple query for the dxnum in each possible location, and all the simple queries are UNIONed together to get a single list.

The second query below is a revision of the first, adding a column for the ICD-10 suggestion, according to the CMS GEMs crosswalk.

SELECT
  dxcode, dxtype, dxdesc
FROM
  sos.dx
WHERE
  dxnum IN
  (
  SELECT dx1 FROM sos.ptcsu
  UNION
  SELECT dx2 FROM sos.ptcsu
  UNION
  SELECT dx3 FROM sos.ptcsu
  UNION
  SELECT dx4 FROM sos.ptcsu
  UNION
  SELECT box67 FROM sos.ptubsu
  UNION
  SELECT box67 FROM sos.ptubsu
  UNION
  SELECT box67 FROM sos.ptubsu
  UNION
  SELECT box67 FROM sos.ptubsu
  UNION
  SELECT box67 FROM sos.ptubsu
  UNION
  SELECT box67 FROM sos.ptubsu
  UNION
  SELECT box67 FROM sos.ptubsu
  UNION
  SELECT box67 FROM sos.ptubsu
  UNION
  SELECT box67 FROM sos.ptubsu
  UNION
  SELECT box67 FROM sos.ptubsu
  UNION
  SELECT fl67i FROM sos.ptubsu
  UNION
  SELECT fl67i FROM sos.ptubsu
  UNION
  SELECT fl67i FROM sos.ptubsu
  UNION
  SELECT fl67i FROM sos.ptubsu
  UNION
  SELECT fl67i FROM sos.ptubsu
  UNION
  SELECT fl67i FROM sos.ptubsu
  UNION
  SELECT fl67i FROM sos.ptubsu
  UNION
  SELECT fl67i FROM sos.ptubsu
  UNION
  SELECT fl67i FROM sos.ptubsu  
  UNION
  SELECT dxnum FROM  sos.v_ptdsmlist WHERE dxnum <> 0
  )
ORDER BY dxcode

The following variation adds a JOIN to the CMS GEMS table to show the suggested ICD-10 code for each of the current ICD-9 codes in use in your data.

SELECT
  a.dxcode, a.dxtype, a.dxdesc, b.icd10 AS "GEMS suggested ICD10", c.longdesc AS "ICD10 Desc" 
FROM
  sos.dx a
  LEFT OUTER JOIN sos.v_ICD9to10 b ON a.dxcode = b.icd9
  LEFT OUTER JOIN sos.dx c ON b.icd10 = c.dxcode
WHERE
  a.dxnum IN
  (
  SELECT dx1 FROM sos.ptcsu
  UNION
  SELECT dx2 FROM sos.ptcsu
  UNION
  SELECT dx3 FROM sos.ptcsu
  UNION
  SELECT dx4 FROM sos.ptcsu
  UNION
  SELECT box67 FROM sos.ptubsu
  UNION
  SELECT box67 FROM sos.ptubsu
  UNION
  SELECT box67 FROM sos.ptubsu
  UNION
  SELECT box67 FROM sos.ptubsu
  UNION
  SELECT box67 FROM sos.ptubsu
  UNION
  SELECT box67 FROM sos.ptubsu   
  UNION   
  SELECT box67 FROM sos.ptubsu   
  UNION   
  SELECT box67 FROM sos.ptubsu
  UNION
  SELECT box67 FROM sos.ptubsu 
  UNION 
  SELECT box67 FROM sos.ptubsu 
  UNION 
  SELECT fl67i FROM sos.ptubsu 
  UNION 
  SELECT fl67i FROM sos.ptubsu 
  UNION 
  SELECT fl67i FROM sos.ptubsu 
  UNION 
  SELECT fl67i FROM sos.ptubsu 
  UNION 
  SELECT fl67i FROM sos.ptubsu 
  UNION 
  SELECT fl67i FROM sos.ptubsu 
  UNION 
  SELECT fl67i FROM sos.ptubsu 
  UNION 
  SELECT fl67i FROM sos.ptubsu 
  UNION 
  SELECT fl67i FROM sos.ptubsu 
  UNION 
  SELECT dxnum FROM  sos.v_ptdsmlist 
  WHERE dxnum <> 0   ) ORDER BY a.dxcode