Skip to content
Advertisement

Converting multiple rows into one row by ID

What I am trying to achieve is group them by id and create a column for the date as well as data.

The background of the dataset are it is lab result taken by participant and some test are not able to be taken on same day due to fasting restrictions n etc. The database I am using is SQL Server.

Below are my DataSet as well as the desired output.

Sample dataset:

Desired output:

enter image description here

Advertisement

Answer

Here’s a solution (that cannot cope with multiple rows of the same id/sample type – you haven’t said what to do with those)

Hopefully you get the idea with this pattern; if you have other sample types with their own dates, break them out of s1 and into their own subquery in a similar way (eg make an s4 for e_chol_1, s5 for k_hcr_1 etc). Note that if any sample type is missing it will cause the whole row to disappear from the results. If this is not desired and you accept NULL for missing samples, use LEFT JOIN instead of INNER

If there will be multiple samples for patient 01 and you only want the latest, the pattern becomes:

Note the addition of row_number() over(partition by id order by lab_date desc) rn – this establishes an incrementing counter in descending date order(latest record = 1, older = 2 …) that restarts from 1 for every different id. We join on it too then say where rn = 1 to pick only the latest records for each sample type

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