Solving my query needs with Rank and may be CrossTab

Iaam Onkara <[email protected]> Sun, 1 Dec 2019 16:54:02 -0600
Newsgroups gmane.comp.db.postgresql.sql
Message-ID <CAMz9UCZFptR4zWdGm0sx2q0_p3K5CMYrPwKPtNP-6Lf_o576Qw@mail.gmail.com>
--000000000000799f370598ac57ba
Content-Type: text/plain; charset="UTF-8"

Hi Friends,

I have a table with data like this gist
https://gist.github.com/daya/d0794efcd4278fc5dce6e7339d03a8fd and I want to
fetch the latest values for a given set of attributes so the result set
looks like this gist
https://gist.github.com/daya/0cb7f8682520a1dd4cdda8c0266f77f6

Please note in the desired result set

   1. There is an assumed mapping of Code to display i.e. code 39156-5 is
   BMI.
   2. "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;

which gives this result
<https://gist.github.com/daya/19ca22a837ed9998c117f38ff3cce3f2>

But the result set that I want
<https://gist.github.com/daya/0cb7f8682520a1dd4cdda8c0266f77f6> I am unable
to get. Or if is it even possible to get?

Thanks for your help

--000000000000799f370598ac57ba
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Hi Friends,<div><br></div><div>I have a table with data li=
ke this gist <a href=3D"https://gist.github.com/daya/d0794efcd4278fc5dce6e7=
339d03a8fd">https://gist.github.com/daya/d0794efcd4278fc5dce6e7339d03a8fd</=
a> and I want to fetch the latest values for a given set of attributes so t=
he result set looks like this gist=C2=A0<a href=3D"https://gist.github.com/=
daya/0cb7f8682520a1dd4cdda8c0266f77f6">https://gist.github.com/daya/0cb7f86=
82520a1dd4cdda8c0266f77f6</a></div><div><br></div><div>Please note in the d=
esired result set=C2=A0</div><div><ol><li>There is an assumed mapping of Co=
de to display i.e. code=C2=A039156-5 is BMI.</li><li>&quot;Oxygen Saturatio=
n&quot; has only one value and &quot;Pulse&quot; has no value</li></ol><div=
>In my attempts and with some help I have=C2=A0 this query=C2=A0</div></div=
><div><br></div><div><font face=3D"monospace">with v_max as <br>(SELECT <br=
>	code, uom, val, created_on, dense_rank() over ( partition by code order b=
y created_on desc) as r <br>FROM vitals v<br>where (v.code =3D &#39;8480-6&=
#39; or v.code=3D&#39;8462-4&#39; or v.code=3D&#39;39156-5&#39; or v.code=
=3D&#39;8302-2&#39;)<br>) <br>SELECT c.display, uom, val, created_on <br>fr=
om v_max v inner join codes c on v.code=3Dc.code<br>where r =3D 1;=C2=A0<br=
></font></div><div><font face=3D"monospace"><br></font></div>which gives <a=
 href=3D"https://gist.github.com/daya/19ca22a837ed9998c117f38ff3cce3f2">thi=
s result</a>=C2=A0<div><br></div><div>But the <a href=3D"https://gist.githu=
b.com/daya/0cb7f8682520a1dd4cdda8c0266f77f6">result set that I want</a>=C2=
=A0I am unable to get. Or if is it even possible to get?</div><div><br></di=
v><div>Thanks for your help</div><div><br></div></div>

--000000000000799f370598ac57ba--