Once you have your string_to_array
you need to unnest
it. Then, you need to join with ICD10
, and, for each patient, take the first
diagnostic.
Assume we have the following data:
CREATE TABLE patients
(
patient_id integer PRIMARY KEY,
diag_list text NOT NULL
) ;
INSERT INTO patients
VALUES
(1, 'A10.3,B55.2, A10.1') ,
(2, 'A10.3, A10.1, C20.2') ;
CREATE TABLE ICD10
(
primary_diag text PRIMARY KEY,
diagnose text
) ;
INSERT INTO ICD10
VALUES
('B55.2', 'Something Bad'),
('A10.1', 'Somehitng Worse');
With this data, we can start to unnest with ordinality
your data:
SELECT
patient_id, trim(diag) AS diag, nr
FROM
patients
JOIN LATERAL unnest(string_to_array(diag_list, ','))
WITH ORDINALITY AS a(diag, nr) ON true ;
and get
+------------+-------+----+
| patient_id | diag | nr |
+------------+-------+----+
| 1 | A10.3 | 1 |
| 1 | B55.2 | 2 |
| 1 | A10.1 | 3 |
| 2 | A10.3 | 1 |
| 2 | A10.1 | 2 |
| 2 | C20.2 | 3 |
+------------+-------+----+
Next step: join this data with ICD10
WITH patients_and_diags AS
(
SELECT
patient_id, trim(diag) AS diag, nr
FROM
patients
JOIN LATERAL unnest(string_to_array(diag_list, ','))
WITH ORDINALITY AS a(diag, nr) ON true
)
SELECT
patient_id, diag, nr, diagnose
FROM
patients_and_diags
JOIN ICD10 ON ICD10.primary_diag = patients_and_diags.diag ;
... and get:
+------------+-------+----+-----------------+
| patient_id | diag | nr | diagnose |
+------------+-------+----+-----------------+
| 1 | B55.2 | 2 | Something Bad |
| 1 | A10.1 | 3 | Somehitng Worse |
| 2 | A10.1 | 2 | Somehitng Worse |
+------------+-------+----+-----------------+
Now we need to take only the smallest 'nr' for each patient_id
The following query, does everything in one step
WITH patients_and_diags AS
(
SELECT
patient_id, trim(diag) AS diag, nr
FROM
patients
JOIN LATERAL unnest(string_to_array(diag_list, ','))
WITH ORDINALITY AS a(diag, nr) ON true
)
, patients_and_ICD10 AS
(
SELECT
patient_id, diag, nr, diagnose
FROM
patients_and_diags
JOIN ICD10 ON ICD10.primary_diag = patients_and_diags.diag
)
, first_ICD10 AS
(
SELECT
patient_id, min(nr) AS nr
FROM
patients_and_ICD10
GROUP BY
patient_id
)
SELECT
patient_id, diag, diagnose
FROM
first_ICD10
JOIN patients_and_ICD10 USING(patient_id, nr) ;
... and get you:
+------------+-------+-----------------+
| patient_id | diag | diagnose |
+------------+-------+-----------------+
| 1 | B55.2 | Something Bad |
| 2 | A10.1 | Somehitng Worse |
+------------+-------+-----------------+
You can check everything at http://rextester.com/RDYPD23700
The query could be made shorter by using some WINDOW
functions; but I think this step-by-step approach is more clear.