Before someone mentions it, I have seen the exact same SQL question on Stack before and from my point of view, that's actually transposing columns to rows. What I'm trying to accomplish is as seen below in the photos, given the top one, I want to create a new table with the previous data that is flipped in this sense.

-
1You can check this stackoverflow.com/q/13372276/6309111Dev-vruper– Dev-vruper2020-12-30 10:06:20 +00:00Commented Dec 30, 2020 at 10:06
-
If I was going there, I wouldn't start from here :-(Strawberry– Strawberry2020-12-30 10:07:17 +00:00Commented Dec 30, 2020 at 10:07
Add a comment
|
1 Answer
You can use the conditional aggregation and union all as follows:
select name_new,
max(case when name = 'PersonA' then A end) as PersonA,
max(case when name = 'PersonB' then A end) as PersonB,
max(case when name = 'PersonC' then A end) as PersonC
from
(select name, 'A1' name_new, A1 A from mytable union all
select name, 'A2' name_new, A2 A from mytable union all
select name, 'A3' name_new, A3 A from mytable union all
select name, 'A4' name_new, A4 A from mytable ) t
group by name_new
11 Comments
Lamtheram
what does name_new represent?
Popeye
see in the sub-query -->
(select name, 'A1' name_new, -- I have just given alias to A1, A2 ... Also check updated answer, In final select I have given alias name to name_newLamtheram
what have i not understood sqlfiddle.com/#!9/997790/4
Popeye
Ok, alias used in the GROUP BY was not correct. I have updated the answer with SQLFiddle'
Lamtheram
one last thing if i was to create a new table with this new format, would I have to go about it manually or? I'm attempting to print all columns that are null for let's say Person A, which is why I was trying to manipulate the table and pass a where clause for null at column Person A
|
