Skip to content

How do I convert columns to rows

I have a table like below, there are two address primary and secondary (some EnId has 3 address):

enter image description here

I need to convert it into a row like below:

enter image description here

I try this SQL statement, but it returns random data.

Attaching Script:

Advertisement

Answer

assuming that you will not have missing row for each set of address, you can use row_number() over (partition by field_name order by id) to generate a number to identify each set of address.

Once you have that, you can use conditional case with aggregrate to do the pivoting.

dbfillde demo

User contributions licensed under: CC BY-SA
10 People found this is helpful