Patients with Identifying Data, Provider, and Visit Count for Specified SortCodes

We have a report that need to be done for the state each month for some of our clients. what I need is a query (to go out to a Lotus or excel spreadsheet) that has first and last names, client ID, DOB, insurance coverage, date of admission (intake) and number of times seen in that month, rendering provider. Ideally we could do this for multiple sort codes but not all of them. For example, I would need it for our sort codes MI, DB, DN, DC but not for the others and if it could be done in one query rather than combining the results of four that would be great.

This query is pretty straightforward, except that you specified RENDERING provider. That would cause multiple lines for each patient account, one for
each provider the patient has seen during the period specified. I have substituted primary provider, but the other is possible if you wanted it (with multiple lines as described).

Another issue is that this query will report dates of service rather than distinct service entries, which could also be done with a slight modification. Further, I have added a condition requiring a fee > 0 (“AND b.amount > 0”), but that may or may not be appropriate in your case. In addition, for your requested “insurance coverage” I have included the payor name and insured’s ID, but just for the first listed policy, if any. You did not specify exactly what you wanted.

Note that the OUTPUT statement you use depends on whether you run the query using dbisqlc or dbisqlg.

SELECT
  a.lastname, a.firstname, a.id, a.dob, a.intakedate,
  e.provcode,
  COUNT(DISTINCT b.trandate) AS "SrvDateCount",
  d.payorname AS "Insurer",
  d.insdid AS "Subscriber#"
FROM
  sos.patients a
  JOIN sos.journal b
  JOIN sos.jcharges c
  LEFT OUTER JOIN sos.rv_policies d ON a.ptnum = d.ptnum
  LEFT OUTER JOIN sos.providers e ON a.providernum = e.providernum
  LEFT OUTER JOIN sos.lookups f ON b.sortcode = f.lunum
WHERE
  b.trandate BETWEEN '2001-01-01' AND '2001-03-31'
  AND d.inspos = 1 //only PRIMARY insurance
  AND f.lucode IN ('MI','DB','DN','DC')
  AND b.amount > 0
GROUP BY
  a.lastname, a.firstname, a.id, a.dob, a.intakedate, e.provcode,"insurer","subscriber#"
;
OUTPUT TO c:\sos\statereport.html FORMAT HTML  

Patients Seen In Period With Insurance And Provider

Is there a query that will list all clients that were seen in a certain time frame along with name of insurance for a specific provider?

Adjust the desired date range and provider code in the WHERE clause below.

SELECT DISTINCT
  a.lastname,
  a.firstname,
  a.id,
  a.provcode,
  (IF c.payortype = 'I' THEN c.payorname ELSE '' ENDIF) AS "Insurance"
FROM
  sos.rv_charges a
  JOIN sos.ptpayors b ON a.ptpayornum = b.ptpayornum
  JOIN sos.payors c ON b.payornum = c.payornum
WHERE
  trandate BETWEEN '2002-01-01' AND '2002-01-31'
  AND a.provcode = '3'
ORDER BY
  a.lastname, a.firstname, a.id

Patient Visits By Provider

Does anyone out there have a query we can use to get data regarding the number of visits a client makes to a provider? We would like to follow how often people come to see our providers. I imagine the query would track by provider, by client, intake date, and subsequent treatment visits.

Take a look at the following, relatively complex query, which delivers the average number of non-zero$ service entries for each provider, plus the itemized list of patients seen by the provider, with the number of services rendered for each patient. The WHERE clauses allow you to specify the selection is for when the patient’s intake date or first non-zero service falls with a specified date range. You must specify the date range in TWO places, and the provider codes to analyze in TWO places. The reason is that there are two queries that are UNIONed into a single result set. One does the details and the other does the average. They won’t match up unless the conditions are set identically for both. This guy also uses DATE, TODAY, and COALESCE functions and the MIN and COUNT aggregate functions, plus an embedded, correlated subquery to determine the date of the first service for each patient. It is definitely not a place to start if you wanted to learn basic SQL, but it has lots of interesting features if you are ready to try your hand at more advanced stuff.

Anyway, you don’t have to understand it to use it! Just paste it into your DBISQL utility, modify the selection criteria, and let ‘er rip.

Note that there is a conceptual gotcha. By including patients who are still in treatment, your results may be invalid. Let’s say that you have a new provider who has only been seeing patients for two months. Assuming weekly visits, her average visits per patient will come up as 8 or fewer. The results are going to be skewed by the number of patients who are still being seen.

I think it is more meaningful to only include patients who have been discharged, so that you are looking at a completed episode of care for each patient. To do so, you would add:

AND (p.flag = 0 OR p.dischargedate IS NOT NULL)

to the two WHERE clauses. That will include only patients in the Inactive list or who have a date in the Discharge Date field. I have done that below, but the lines won’t execute unless you remove the two slashes at the beginning of the additional condition in the WHERE clause of both of the main queries.

SELECT
  v.provcode,
  p.lastname,p.firstname,p.id,
  DATE(COALESCE(intakedate,(SELECT MIN(trandate)
                            FROM sos.journal
                            WHERE ptnum = p.ptnum
                            AND trantype='S' AND amount > 0)
       ,'1900-01-01')) AS "Initial",
  COUNT(DISTINCT j.jnum) AS "Visits"
FROM
  sos.patients p
  JOIN sos.journal j ON p.ptnum = j.ptnum
  JOIN sos.jcharges c ON j.jnum = c.jnum
  JOIN sos.providers v ON c.providernum = v.providernum
WHERE
  "Initial" BETWEEN '1995-01-01' AND TODAY()
  AND v.provcode IN ('A','B','C')
  //AND (p.flag = 0 OR p.dischargedate IS NOT NULL)
GROUP BY
  v.provcode,p.lastname,p.firstname,p.id,"Initial"
UNION
SELECT
  v.provcode,
  '--AVERAGE','NUMBER OF','VISITS',
   NULL,
  COUNT(DISTINCT j.jnum)/COUNT(DISTINCT j.ptnum) AS "Avg Vists"
FROM
  sos.patients p
  JOIN sos.journal j ON p.ptnum = j.ptnum
  JOIN sos.jcharges c ON j.jnum = c.jnum
  JOIN sos.providers v ON c.providernum = v.providernum
WHERE
  DATE(COALESCE(intakedate,(SELECT MIN(trandate)
                            FROM sos.journal
                            WHERE ptnum = p.ptnum
                            AND trantype='S'
                            AND amount > 0)
       ,'1900-01-01')) BETWEEN '1995-01-01' AND TODAY()
  AND v.provcode IN ('A','B','C')
  //AND (p.flag = 0 OR p.dischargedate IS NOT NULL)
GROUP BY
  v.provcode
ORDER BY
  1,2,3,4

Service Count By Provider, Patient Category and Service Code For Period

I need total # of patients seen, broken down by provider, patient category, and service code, for a specific time frame.

SELECT
(a.provfname + ' '+a.provlname) AS "Provider",
c.categcode AS "Category",
a.srvcode AS "Service",
COUNT(DISTINCT a.ptnum) AS "Pt Count"
FROM
sos.rv_charges a
JOIN sos.patients b ON a.ptnum = b.ptnum
LEFT OUTER JOIN sos.ptcategs c ON b.ptcategnum = c.ptcategnum
WHERE
a.trandate BETWEEN '1980-01-01' AND '2008-12-31'
GROUP BY
"Provider","Category","Service"

Outstanding Account Cleanup

Would it be possible to write a query to do the following:

List the patient name, account number, outstanding balance and provider
For any account that has not had a date of service in 2009
And has had no payments within the past 30 days.

We want to use this to clear out all such outstanding accounts..

The views used in the query below are not super-efficient, so on a large database it will take a good while to run, but it will deliver the results you want.

SELECT
a.lastname + ', '+ a.firstname AS "Name",
a.id AS "Account",
c.provcode AS "Primary-Provider",
(SELECT sos.LASTCHARGEDATE(a.ptnum)) AS "LastService",
(SELECT sos.LASTCREDITDATE(a.ptnum)) AS "LastPayment",
d.ptbalance AS "Balance"
FROM sos.patients a
LEFT OUTER JOIN sos.providers c ON a.providernum = c.providernum
JOIN sos.patientbalance d ON a.ptnum = d.ptnum
WHERE
"LastService" < '2009-01-01'
AND ("LastPayment" < (TODAY()-30) OR "LastPayment" IS NULL )
ORDER BY
"Name", "Account"
;
OUTPUT TO c:\sos\cleanup.html FORMAT HTML
;