There is an assumed mapping of Code to display i.e. code 39156-5 is BMI.
"Oxygen Saturation" has only one value and "Pulse" has no value
In my attempts and with some help I have this query
with v_max as (SELECT code, uom, val, created_on, dense_rank() over ( partition by code order by created_on desc) as r FROM vitals v where (v.code = '8480-6' or v.code='8462-4' or v.code='39156-5' or v.code='8302-2') ) SELECT c.display, uom, val, created_on from v_max v inner join codes c on v.code=c.code where r = 1;
I take it the last value by timestamp per code per patient is the one to be reported? Or is there a time window?
Turning rows into columns can be done with sub-selects per derived column or (usually faster) temporary tables built up in separate selects with each adding usually one column (but possibly more).