Patient List with Dates of Referral, Intake, and Discharge

“I need a listing of:
Active clients for a specific Patient category (one of our sites, #103), that will list:
1. Date of referral (and I want to be able to speccify all referrals on this date or later (in this case 5/1/04)
2. Date of intake (and I want to be able to specify all intakes on this date or later, in this case 5/1/04)
3. Date of discharge (and leaving blank or whatever if client has NOT been discharged
4. The entry in User-Sort 3 (being able to specify which one), in this case "MHP" or "GZ" or "PA" or
"MED" or "MED/MHP" or "MED/INS"
5. The entry (if there is one) in User-defined (client) field #3”
As you have already realized, SQL is very "plain language" ish. It basically makes sense when you read
it, even if you aren't familiar with the language -- at least up to a point. The one thing that might throw
you a little are the correlation names. Basically, we give table names a short alias to avoid retyping the
name over and over. Here is the query:
SELECT 
   a.LastName, 
   a.FirstName, 
   a.ID, 
   a.ReferralDate, 
   a.IntakeDate, 
   a.DischargeDate, 
   a.UserSort3,
   c.Fld3
FROM 
   sos.Patients a
   JOIN sos.PtCategs b ON a.ptcategnum = b.ptcategnum
   LEFT OUTER JOIN sos.UDDataPt c ON a.UDDataNum = c.UDDataNum
WHERE 
   b.CategCode = 'ABC'
   AND a.flag = 0
   AND a.LicNum = 103
   AND a.ReferralDate >= '2004-05-01'
   AND a.IntakeDate >= '2004-05-01'
   AND a.UserSort3 IN ('MHP','GZ','PA','MED','MED/MHP','MED/INS')
ORDER BY 
   a.LastName, 
   a.FirstName, 
   a.ID
Notice that each table named in the FROM clause is followed by a single letter. That creates the alias,
which can be used even in the SELECT list that precedes the FROM clause. By saying...
FROM Patients a
We can now use "a" wherever complete syntax would dictate that we would have to use the entire table
name. We can therefore do...
ORDER BY a.LastName, a.FirstName, a.ID
Instead of...
ORDER BY Patients.LastName, Patients.FirstName, Patients.ID
Additional notes:
The other thing that is not intuitive, and that requires a good understanding of the structure of the
database with which you are working, is when to relate tables with a JOIN rather than a LEFT OUTER
JOIN or a RIGHT OUTER JOIN. In your query we are JOINing Patients to PtCategs because you have
specified that you will be selecting based on membership in a particular category. If a patient has not
been assigned to a category, you don't want them in the result set. The JOIN essentially makes a match
between Patients and PtCategs required. That is, there must be matching data in the specified fields or
the row will not be included in the results. (For more on how we join tables together, the last chapter of
OMTECH.PDF or SOSTECH.PDF in your SOS folder is an absolute must.) I don't know what your
categories are, so I just made up a category code "ABC" and use that as one of the selection criteria. A
LEFT OUTER JOIN between these tables with everything else the same, would result in both ABC
category patients but also those patients who have not been assigned to any category -- VERY different
results! In the case of the User Defined Data, which is held in the related table UDDataPt, we want to
include those patients who do have a UDData record, but also those who may not, so we use the OUTER
type of JOIN. Some people "get" this stuff right away; others don't. Any intro to SQL or relational
databases should include a more thorough explanation.
Let me go through the WHERE conditions in order. Think of these as your filter or selection criteria.
WHERE
b.CategCode = 'ABC'
...the patient must be assigned to patient category "ABC" on the Additional tab of the Patient form
AND a.flag = 0
...the patient must be in the Active Patients list
AND a.LicNum = 103
...the patient must be in data set (also called "sublicense") number 103
AND a.ReferralDate >= '2004-05-01'
...the patient must have a referral date and it must be 5/1/04 or later
AND a.IntakeDate >= '2004-05-01'
...the patient must have an intake date and it must be 5/1/04 or later
AND a.UserSort3 IN ('MHP','GZ','PA','MED','MED/MHP','MED/INS')
...the patient must have a value in the UserSort 3 field and it must be one of the values specified in the
list between the parentheses.


	

Last Payments

You asked for a report that includes last payment information. This information is readily available in the
database, but we have never included it in any of the standard reports.

SELECT
   (a.Lastname + ', ' + a.Firstname) AS "PtName",
   a.id,
   TRIM(c.firstname + ' '+c.payorname) AS "Payor",
   b.lastpaydate,
   b.lastpayamt,
   b.postedbal
FROM
   sos.patients a
   JOIN sos.ptpayors b ON a.ptnum = b.ptnum
   JOIN sos.payors c ON b.payornum = c.payornum
WHERE
   postedbal > 0

Screening Today’s Appointments for Outstanding Balances

We would like to do a daily query to alert us to any patients with an appointment for today who have an outstanding balance of $120 or more and have been billed at least once for unpaid services.

This is a pretty straight-forward query, with the exception of a subquery in the WHERE clause. The subquery determines if there are any non-insurance chargesplits carrying a balance that show a billing date, indicating that there has been at least some billing done for one or more of the outstanding services.

SELECT DISTINCT
  a.SITENAME + ' (' + a.SITECODE + ')' As site,
  (a.PROVLNAME + ', ' + a.PROVFNAME + ' (' + a.PROVCODE + ')') As provider,
  a.PTFULLNAME,
  a.ID,
  b.PTBALANCE,
  a.APPTDATE
FROM
  sos.rv_appts a
  JOIN sos.pt_noninsbalance b ON a.PTNUM = b.PTNUM
WHERE
  b.ptbalance >= 120
  AND a.apptdate = Today()
  AND (SELECT COUNT(*)
       FROM sos.jchgsplits cs
         JOIN sos.ptpayors ptp ON  cs.ptpayornum = ptp.ptpayornum
         JOIN sos.payors pay ON ptp.payornum = pay.payornum
       WHERE cs.ptnum = a.ptnum
         AND pay.payortype <> 'I'
         AND cs.ChgSplBal > 0
         AND cs.lastbilled IS NOT NULL ) > 0
ORDER BY
  site, provider, a.ptfullname, a.id

Patients With Specified Diagnosis, Intake Date, And Last Date of Service

I want to see a list of clients bearing a certain diagnosis, along with their intake date, each of their four diagnoses, and last date of service.

SELECT
  Providers. ProvCode, Providers.ProvLName, Providers.ProvFName,
  Patients.LastName, Patients.FirstName, Patients.IntakeDate,
  PtVars.LFeeDate AS "Last Service",
  PtCSUDx.DxCode1, PtCSUDx.DxCode2, PTCSUDx.DxCode3, PtCSUDx.DxCode4  
FROM
  sos.Patients
  LEFT OUTER JOIN sos.Providers ON Patients.ProviderNum = Providers.ProviderNum
  LEFT OUTER JOIN sos.PtCSU ON Patients.PtNum = PtCSU.PtNum
  LEFT OUTER JOIN sos.PtCSUDx ON PtCSU.PtCSUNum = PtCSUDx.PtCSUNum  
  LEFT OUTER JOIN sos.PtVars ON Patients.PtNum = PtVars.PtNum  
WHERE
  Patients.LicNum = 101 AND
  PtCSU.TypeFlag = 'D' AND
  PtCSUDx.DxCode1 = '300.14'   /*<-- desired Dx code goes here*/
ORDER BY
  Providers.ProvCode, Patients.LastName

Patients with Diagnosis in Any Position, By Provider

We have been asked to give statistics regarding how many dual diagnosis clients we treat during a specific time period. I can run a report requesting the primary diagnosis; however, at times I need to run a report on one specific diagnosis that may be listed as #2, 3 or even 4.

SELECT
  Providers. ProvCode, Providers.ProvLName, Providers.ProvFName,
  Patients.LastName, Patients.FirstName, Patients.IntakeDate,
  PtVars.LFeeDate AS "Last Service",
  PtCSUDx.DxCode1, PtCSUDx.DxCode2, PTCSUDx.DxCode3, PtCSUDx.DxCode4  
FROM
  sos.Patients
  LEFT OUTER JOIN sos.Providers ON Patients.ProviderNum = Providers.ProviderNum
  LEFT OUTER JOIN sos.PtCSU ON Patients.PtNum = PtCSU.PtNum
  LEFT OUTER JOIN sos.PtCSUDx ON PtCSU.PtCSUNum = PtCSUDx.PtCSUNum  
  LEFT OUTER JOIN sos.PtVars ON Patients.PtNum = PtVars.PtNum  
WHERE
  Patients.LicNum = 101
  AND PtCSU.TypeFlag = 'D'  
  AND (SELECT COUNT(*)
            FROM sos.journal
            /* service date range goes on next line */
            WHERE trantype = 'S' AND amount > 0 AND trandate BETWEEN '2000-01-01' AND '2008-06-30') > 0
  /*<-- desired Dx code goes on next line*/
  AND 'F10.150' IN (PtCSUDx.DxCode1, PtCSUDx.DxCode2, PTCSUDx.DxCode3, PtCSUDx.DxCode4)    
ORDER BY
  Providers.ProvCode, Patients.LastName, Patients.FirstName