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
AWSSQL

Avatar of undefined
Last Comment
Ollie H

8/22/2022 - Mon
lcohan

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

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes