Link to home
Start Free TrialLog in
Avatar of Ollie H
Ollie H

asked on

Athena AWS SQL

HI,

Hi I have done some calculations and have all my results in one row across a number of columns.

 Percent 1 percent 2, percent3

I would like to pivot these so i have 2 columns,

Percents.      Total

Percent1 0.64
Percent2 0.22
Etc
Avatar of lcohan
lcohan
Flag of Canada image

You should be able to use the UNPIVOT function to convert the columns into rows - something like in pseudo code below:
select id, 
  Percentname,
  Percentvalue
from yourtable
unpivot
(
  Percentvalue
  for Percentname in (Percent1, Percent2, Percent3)
) unpiv;

Open in new window



You could also use CROSS APPLY with UNION ALL to convert the columns:

select id, 
  Percentname,
  Percentvalue
from yourtable
cross apply
(
  select 'Percent1', Percent1 union all
  select 'Percent2', Percent2 union all
  select 'Percent3', Percent3 --union all
  -- select 'Percent4', Percent4 .....
) c (Percentname, Percentvalue);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ollie H
Ollie H

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial