I need to build a query that will aggregate the # of units of blood that has been transfused so it can be compared to the # of units of blood that has been cross-matched. Blood (a precious resource) that is cross-matched, but not transfused is wasted.
Providers are supposed to check the system-of-record (Epic) for ‘active’ cross-match orders before creating new ones. Providers that don’t do this are ‘penalized’ (provider 20). No penalty applies (it seems) for providers that don’t transfuse all of the blood that they’ve cross-matched (provider 10).
Cross-match orders:
|ENC_ID|PROV_ID|ORDER_ID|ORDER_TIME |UNITS|
| 1| 10| 100|26-JUL-12 13:00| 4|
| 1| 20| 231|26-JUL-12 15:00| 2|
Transfusion orders:
|ENC_ID|PROV_ID|ORDER_ID|ORDER_TIME |UNITS|
| 1| 10| 500|26-JUL-12 13:05| 1|
| 1| 10| 501|26-JUL-12 13:25| 1|
| 1| 20| 501|26-JUL-12 15:00| 1|
| 1| 20| 501|26-JUL-12 15:21| 2|
Rules:
- compare transfusions to cross-matches for same encounter (
transfusion.END_ID=cross-match.ENC_ID) - transfusion orders are applied to cross-match orders in a FIFO manner
transfusion.ORDER_TIME >= cross-match.ORDER_TIME- a provider may transfuse more than their cross-match order, as long as all of the ‘active’ cross-match order still have available units (provider 20’s second transfusion order)
Desired result:
|ENC_ID|PROV_ID|ORDER_ID|ORDER_TIME |CROSS-MATCHED|TRANSFUSED|
| 1| 10| 100|26-JUL-12 13:00| 4| 4|
| 1| 20| 231|26-JUL-12 15:00| 2| 1|
Provider 10 ‘credited’ with Provider 20’s transfusions.
Can this logic be implemented without resorting to a procedure?
You could do it in a single SQL query. Here’s an example (tested on 11gR2, should work on 10g):
SETUP:
The following query will build a list of blood units numerically and join each unit from the
cross_matchtable to the corresponding one (if it exists) in thetransfusiontable:This may be efficient if the maximum number of units remains small, otherwise this 1-to-1 relationship may become cumbersome.
This can be improved by using a running total of units on both sides instead of a basic 1-1. The join condition would be like an interval intersection between begin unit and end unit: