I'm currently trying to write a query for an oracle DB to get some information about a patient's hospital stay. For every day he gets a specific score in specific fields - therefore I have to join multiple tables to get the actual daily score (not the full score).
My first attempt (which is working, but I want to show it in another format) was this:
SELECT
f.FALLID AS "ID",
f.FALLNR AS "Fallnummer",
DECODE(e.SCOREID, 12, 'SAPS', 13, 'TISS', 'X') AS "Score",
to_char(e.VON, 'DD.MM.YYYY') AS "Datum (von)",
-- to_char(e.BIS, 'DD.MM.YYYY') AS "Datum (bis)",
scp.BEZEICHNUNG AS "Bezeichnung",
s.PUNKTE AS "Punkte"
FROM
FALL f
JOIN OS_MUP.EINSTUFUNG e ON e.FALLID = f.FALLID
JOIN OS_MUP.EINSTUFUNGSRANG einr ON einr.EINSTUFUNGID = e.EINSTUFUNGID
JOIN OS_MUP.SCORERANG s ON s.SCORERANGID = einr.SCORERANGID
JOIN OS_MUP.SCOREPOSITION scp ON scp.SCOREPOSITIONID = s.SCOREPOSITIONID
WHERE
f.FALLNR = 1234567
ORDER BY
e.SCOREID, e.VON
This is showing the results in the following form:
+------+---------+-------+------------+-------------+--------+
| ID | Fallnr | Score | Date | Description | Points |
+------+---------+-------+------------+-------------+--------+
| 1234 | 1234567 | ABCD | 05.05.2020 | Age | 12 |
| 1234 | 1234567 | ABCD | 05.05.2020 | Temperature | 2 |
+------+---------+-------+------------+-------------+--------+
That's okay, but I don't want the repeating column for the date and the description for every day, I wanted to get like that appearance:
+------+---------+-------+------------+-----+-------------+
| ID | Fallnr | Score | Date | Age | Temperature |
+------+---------+-------+------------+-----+-------------+
| 1234 | 1234567 | ABCD | 05.05.2020 | 12 | 2 |
| 1234 | 1234567 | ABCD | 06.05.2020 | 12 | 2 |
+------+---------+-------+------------+-----+-------------+
So I reworked the query and finished with this at the moment:
variable var number
exec :var := 2068237610
SELECT
dt1.FALLNR AS "Fallnummer",
to_char(dt1.VON, 'DD.MM.YYYY') AS "Datum",
dt1.PUNKTE AS "Alter",
dt2.PUNKTE AS "Herzfrequenz"
--dt3.PUNKTE AS "RR Syst.",
--dt4.PUNKTE AS "Körpertemp.",
--dt5.PUNKTE AS "PaO2/FiO2"
-- dt6.PUNKTE AS "Urin",
-- dt7.PUNKTE AS "Leukozyten"
FROM (
SELECT
f.FALLID,
f.FALLNR,
s.PUNKTE,
e.VON
FROM Fall f
JOIN OS_MUP.EINSTUFUNG e ON e.FALLID = f.FALLID
JOIN OS_MUP.EINSTUFUNGSRANG einr ON einr.EINSTUFUNGID = e.EINSTUFUNGID
JOIN OS_MUP.SCORERANG s ON s.SCORERANGID = einr.SCORERANGID
WHERE
s.SCOREPOSITIONID = 10003 AND f.FALLNR = :var /* Alter */
) dt1
JOIN
(SELECT
f.FALLNR,
s.PUNKTE,
e.VON
FROM Fall f
JOIN OS_MUP.EINSTUFUNG e ON e.FALLID = f.FALLID
JOIN OS_MUP.EINSTUFUNGSRANG einr ON einr.EINSTUFUNGID = e.EINSTUFUNGID
JOIN OS_MUP.SCORERANG s ON s.SCORERANGID = einr.SCORERANGID
WHERE
s.SCOREPOSITIONID = 10004 AND f.FALLNR = :var /* Herzfrequenz */
) dt2
ON dt1.FALLNR = dt2.FALLNR AND dt1.VON = dt2.VON
ORDER BY dt1.VON
This is working as I want it to work, but I'd have to do those joins over and over again for more columns, and there are like 5-6 more to come - and cause of the nested loops created by the joins this version of the query is extremely slow (like 250 seconds slow).
Is there a chance to achieve my goal in another way of with less use of joins?
Thanks in advance!