Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL select most recent timestamp before order

I have two tables: one with the order time of a specific procedure for a group of patients, with some patients having the same type of order (with a different order ID) more than once per stay:PATIENT_ORDERS:

INDEX_ID VISIT_ID PATIENT_ID ORDER_ID ORDER_TIME
1 10 1 100 6/1/22 8:19AM
2 10 1 101 6/5/22 4:30PM
1 22 2 109 8/7/22 10:30AM
1 50 3 111 9/2/22 11:00AM

and the other with the arrival time in hospital departments for the same group of patients: PATIENT_TRANSFERS:

VISIT_ID DEP_NAME ADT ADT_TIME
1 UnitA Admission 5/25/22 6:00AM
1 OR Transfer In 5/30/22 9:00AM
1 UnitA Transfer In 5/30/22 9:00PM
1 OR Transfer In 6/5/22 12:00PM
1 UnitA Transfer In 6/5/22 4:00PM
22 UnitB Admission 8/6/22 9:00PM
22 OR Transfer In 8/7/22 1:00AM
22 UNITB Transfer In 8/7/22 9:00AM
50 UnitA Admission 9/1/22 12:00PM
50 OR Transfer In 9/2/22 8:00AM
50 Unit Transfer In 9/2/22 10:00AM

I want to join the tables to show the time of the most recent transfer into a department after the OR, but before the time of the procedure order.
For patients that have more than one procedure order per stay, I want to show the most recent transfer into a department before the second procedure order time.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

EXPECTED OUTPUT:

INDEX_ID VISIT_ID PAT_ID ORDER_ID ORDER_TIME ARRIVAL_TIME NAME
1 10 1 100 6/1/22 8:19AM 5/30/22 9:00PM UnitA
2 10 1 101 6/5/22 4:30PM 6/5/22 4:00PM UnitA
1 22 2 109 8/7/22 10:30AM 8/7/22 9:00AM UnitB
1 50 3 111 9/2/22 11:00AM 9/2/22 10:00AM UnitA

I am not sure how to approach this problem. I know that I need max(adt_time)<order_time, but I am not sure what the syntax would be, particularly for those patients with more than one order_time per visit.

Using standard sql.

Thank you!

>Solution :

You can use a CORRELATED SUBQUERY to accomplish this. In this example, I return the MAX(ADT_TIME) where the ADT_TIME < ORDER_TIME.

Important Notes:

  • In the example below, I had to fix the VISIT_ID in the PATIENT_TRANSFERS table since the value was 1 instead of 10.
  • I did this in MySQL since your DBMS was not specified, but you should be able to alter the syntax to your corresponding DBMS if it doesn’t work already. Ultimately, the idea and gist of how you can accomplish this is there.

Schema (MySQL v8.0)

CREATE TABLE PATIENT_ORDERS (
  `INDEX_ID` INTEGER,
  `VISIT_ID` INTEGER,
  `PATIENT_ID` INTEGER,
  `ORDER_ID` INTEGER,
  `ORDER_TIME` DATETIME
);

INSERT INTO PATIENT_ORDERS
  (`INDEX_ID`, `VISIT_ID`, `PATIENT_ID`, `ORDER_ID`, `ORDER_TIME`)
VALUES
  ('1', '10', '1', '100', '2022-06-01 08:19:00'),
  ('2', '10', '1', '101', '2022-06-05 16:30:00'),
  ('1', '22', '2', '109', '2022-08-07 10:30:00'),
  ('1', '50', '3', '111', '2022-09-02 11:00:00');

CREATE TABLE PATIENT_TRANSFERS (
  `VISIT_ID` INTEGER,
  `DEP_NAME` VARCHAR(5),
  `ADT` VARCHAR(11),
  `ADT_TIME` DATETIME
);

INSERT INTO PATIENT_TRANSFERS
  (`VISIT_ID`, `DEP_NAME`, `ADT`, `ADT_TIME`)
VALUES
  ('10', 'UnitA', 'Admission', '2022-05-25 06:00:00'),
  ('10', 'OR', 'Transfer In', '2022-05-30 09:00:00'),
  ('10', 'UnitA', 'Transfer In', '2022-05-30 21:00:00'),
  ('10', 'OR', 'Transfer In', '2022-06-05 12:00:00'),
  ('10', 'UnitA', 'Transfer In', '2022-06-05 16:00:00'),
  ('22', 'UnitB', 'Admission', '2022-08-06 21:00:00'),
  ('22', 'OR', 'Transfer In', '2022-08-07 01:00:00'),
  ('22', 'UNITB', 'Transfer In', '2022-08-07 09:00:00'),
  ('50', 'UnitA', 'Admission', '2022-09-01 12:00:00'),
  ('50', 'OR', 'Transfer In', '2022-09-02 08:00:00'),
  ('50', 'Unit', 'Transfer In', '2022-09-02 10:00:00');

Query #1

SELECT a.INDEX_ID,
       a.VISIT_ID,
       a.PATIENT_ID,
       a.ORDER_ID,
       a.ORDER_TIME,
       b.ADT_TIME,
       b.DEP_NAME
FROM PATIENT_ORDERS a
INNER JOIN PATIENT_TRANSFERS b ON a.VISIT_ID = b.VISIT_ID
WHERE b.ADT_TIME =
  (SELECT MAX(ADT_TIME)
   FROM PATIENT_TRANSFERS
   WHERE visit_id = b.visit_id
     AND ADT_TIME < a.ORDER_TIME);
INDEX_ID VISIT_ID PATIENT_ID ORDER_ID ORDER_TIME ADT_TIME DEP_NAME
1 10 1 100 2022-06-01 08:19:00 2022-05-30 21:00:00 UnitA
2 10 1 101 2022-06-05 16:30:00 2022-06-05 16:00:00 UnitA
1 22 2 109 2022-08-07 10:30:00 2022-08-07 09:00:00 UNITB
1 50 3 111 2022-09-02 11:00:00 2022-09-02 10:00:00 Unit

View on DB Fiddle

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading