Skip to content
Advertisement

Find the lowest score after the first Highest Score

I am looking for a SQL query.

Table 1: Source Table

enter image description here

Table 2: Result Table

enter image description here

Attached the patient table. Here I want to find the Highest Score, Highest Score Date, Lowest Score and Lowest Score Date of each patient. The tricky part is that if a patient has same highest score (here it is 9) on two different dates (10/5/2018 and 8/4/2020), we need to take earliest high score (10/5/2018). Similarly, if the patient has same lowest score (6 here) on two different dates (3/1/2019 and 4/2/2020) we should take the latest low score (4/2/2020)

Table 1: Source table contains all scores of a single patient. Patient ID is the primary key of that table. I want a result table that look like table 2.

I have tried this

But this code can show me highest or the lowest score. But it does not meet all my requirement.

Advertisement

Answer

If there is table t with columns: PatientName, PatientID, ServiceDate, Score. Something like this:

After update to question:

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement