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
-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>