Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8313993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:40:39+00:00 2026-06-08T20:40:39+00:00

I need to build a query that will aggregate the # of units of

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T20:40:40+00:00Added an answer on June 8, 2026 at 8:40 pm

    You could do it in a single SQL query. Here’s an example (tested on 11gR2, should work on 10g):

    SETUP:

    CREATE TABLE cross_match as (
       SELECT 1 ENC_ID, 10 PROV_ID, 100 ORDER_ID, 
              to_date('2012-07-26 13', 'yyyy-mm-dd hh24') ORDER_TIME, 4 UNITS 
         FROM DUAL
       UNION ALL SELECT 1, 20, 231, to_date('2012-07-26 15', 'yyyy-mm-dd hh24'), 2 FROM DUAL
    );
    CREATE TABLE transfusion as (
       SELECT 1 ENC_ID, 10 PROV_ID, 500 ORDER_ID, 
              to_date('2012-07-26 13:05', 'yyyy-mm-dd hh24:mi') ORDER_TIME, 1 UNITS 
              FROM DUAL
       UNION ALL SELECT 1, 10, 501, to_date('2012-07-26 13:25', 'yyyy-mm-dd hh24:mi'), 1 FROM DUAL
       UNION ALL SELECT 1, 20, 501, to_date('2012-07-26 15:00', 'yyyy-mm-dd hh24:mi'), 1 FROM DUAL
       UNION ALL SELECT 1, 20, 501, to_date('2012-07-26 15:21', 'yyyy-mm-dd hh24:mi'), 2 FROM DUAL
    );  
    

    The following query will build a list of blood units numerically and join each unit from the cross_match table to the corresponding one (if it exists) in the transfusion table:

    WITH cross_order as (
       SELECT rownum rn FROM DUAL
       CONNECT BY level <= (SELECT MAX(units) FROM cross_match)
    ),
    transfusion_order as (
       SELECT rownum rn FROM DUAL
       CONNECT BY level <= (SELECT MAX(units) FROM transfusion)
    )
    SELECT c.enc_id, c.prov_id, c.order_id, c.order_time, 
           count(*) cross_matched,
           count(t.enc_id) transfused
      FROM (SELECT cm.*, 
                   row_number() over (partition by cm.enc_id 
                                      order by cm.order_time) cross_no
              FROM cross_match cm
              JOIN cross_order co ON cm.units >= co.rn) c
      LEFT JOIN (SELECT t.*, 
                        row_number() over (partition by t.enc_id 
                                           order by t.order_time) trans_no
                   FROM transfusion t
                   JOIN transfusion_order tor ON t.units >= tor.rn) t
             ON c.enc_id = t.enc_id
                AND c.cross_no = t.trans_no
     GROUP BY c.enc_id, c.prov_id, c.order_id, c.order_time;
    
    ENC_ID PROV_ID ORDER_ID ORDER_TIME CROSS_MATCHED TRANSFUSED
    -----------------------------------------------------------
    1      20      231      07/26/2012             2          1
    1      10      100      07/26/2012             4          4
    

    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:

    SELECT c.enc_id, c.prov_id, c.order_id, c.order_time, 
           sum(c.unit_end - nvl(c.unit_start,0))/count(*) cross_matched,
           sum(least(c.unit_end, t.unit_end)
               -greatest(nvl(c.unit_start, 0), nvl(t.unit_start, 0))) transfused
      FROM (SELECT cm.*, 
                   sum(cm.units) over (partition by cm.enc_id 
                                      order by cm.order_time
                                      rows between unbounded preceding
                                               and 1 preceding) unit_start,
                   sum(cm.units) over (partition by cm.enc_id 
                                      order by cm.order_time) unit_end
              FROM cross_match cm) c
      LEFT JOIN (SELECT t.*,
                   sum(t.units) over (partition by t.enc_id 
                                      order by t.order_time
                                      rows between unbounded preceding
                                               and 1 preceding) unit_start, 
                        sum(t.units) over (partition by t.enc_id 
                                           order by t.order_time) unit_end
                   FROM transfusion t) t
             ON c.enc_id = t.enc_id
                AND c.unit_end > nvl(t.unit_start, 0)
                AND t.unit_end > nvl(c.unit_start, 0)
     GROUP BY c.enc_id, c.prov_id, c.order_id, c.order_time;
    
    ENC_ID PROV_ID ORDER_ID ORDER_TIME CROSS_MATCHED TRANSFUSED
    -----------------------------------------------------------
    1      20      231      07/26/2012             2          1
    1      10      100      07/26/2012             4          4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to build a sqlserver query that if a text column has a
I need to build a query that will show me records that are in
I need to build a Linq query that will show the results as follow:
I'm trying to build a query in Postgresql that will be used for a
I need to write a query that will perform a keyword search on a
I'm trying to build a query that will order results by date and time,
I need some help making a linq query that will select a list of
I have read-only access to these tables, and need to build a query that
I'm trying to build an asp.net page using c# that will query a column
I am trying to create a function that will build it's own query with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.