I am getting data in the following format from a sql table:
DisplayName PropertySystemName PropertyDefaultName PropertyValue
S1 P1 Property 1 Value 1
S1 P2 Property 2 Value 2
S1 P3 Property 3 Value 3
S1 P4 Property 4 Value 4
S1 P5 Property 5 Value 5
S1 P6 Property 6 Value 6
S1 P7 Property 7 Value 7
S1 P8 Property 8 Value 8
S1 P9 Property 9 Value 9
S1 P10 Property 10 Value 10
This is the desired output:
DisplayName Property 1 Property 2 Property 3
S1 Value 1 Value 2 Value 3
This is the query i have but it does not produce the desired output.
Select me.DisplayName,
Min(Case PropertySystemName When 'P1' Then PropertyValue End) PropertyDefaultName,
Min(Case PropertySystemName When 'P2' Then PropertyValue End) PropertyDefaultName,
Min(Case PropertySystemName When 'P3' Then PropertyValue End) PropertyDefaultName
FROM vManagedEntity me
This is the output of above query:
DisplayName PropertyDefaultName PropertyDefaultName PropertyDefaultName
S1 Value 1 Value 2 Value 3
How do i modify the query in order to produce the desired output without hard coding the Column Header as it is already present in the table.
You cannot use the value from the
PropertyDefaultNamecolumn as a column header unless you use dynamic sql. Using Dynamic SQL will allow you to pull the column values and the header names directly from the tables:See SQL Fiddle with demo
The result is: