Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Selected values from a table to be represented as columns by a query in Oracle

I have this table

  -visitId- | -vital-              |-value-
    43475   |   vital-pulse        |    1
    42818   |   vital-tempreture   |    37
    42818   |   vital-pulse        |    37
    43475   |   vital-tempreture   |   103
    57126   |   vital-tempreture   |    37.5
    57126   |   vital-pulse        |    5

All the values in the column -vital- belongs to vital-pulse and vital-temperature I want these 2 categories to be 2 columns with respective vital values against each column showing with the visit ID. I tried pivot aggregating with -visitId- column, but how to have it in final output?

Desired output as following

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  -visitId- | -vital-tempreture-   |-vital-pulse-    |
    42818   |      37              |    37           |
    43475   |      103             |     1           |
    57126   |      37.5            |     5           |

>Solution :

Conditional aggregation seems to be simple enough:

Sample data:

SQL> with test (visitid, vital, value) as
  2    (select 43475, 'vital-pulse'     , 1    from dual union all
  3     select 42818, 'vital-tempreture', 37   from dual union all
  4     select 42818, 'vital-pulse'     , 37   from dual union all
  5     select 43475, 'vital-tempreture', 103  from dual union all
  6     select 57126, 'vital-tempreture', 37.5 from dual union all
  7     select 57126, 'vital-pulse'     , 5    from dual
  8    )

Query begins here:

  9  select visitid,
 10    max(case when vital = 'vital-tempreture' then value end) vital_tempreture,
 11    max(case when vital = 'vital-pulse'      then value end) vital_pulse
 12  from test
 13  group by visitid
 14  order by visitid;

   VISITID VITAL_TEMPRETURE VITAL_PULSE
---------- ---------------- -----------
     42818               37          37
     43475              103           1
     57126             37,5           5

SQL>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading