Skip to content
Advertisement

How can I convert an EAV schema to a normal schema using PIVOT?

I have a table called SourceTable, in that I have 4 fields. Properties_title field it has got 3 values (AAA,BBB,CCC) but can also have more. Depending on each of them, NumericValue field and Property_item_title field has a value.According to the table in the below, if Properties_title be AAA or CCC so Property_item_title it has value and if Properties_title be BBB so NumericValue it has value. Now I want pivot this to make just one row for each W_ID like Result Table.

SourceTable:

Result Table:

the column names has to be dynamic

My Code:

Here is a db<>fiddle.

Thank you for your help.

Advertisement

Answer

To pivot over a fixed list of columns, you can do conditional aggregation:

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