I have a TSQL table that contains a number of columns, where I would like to focus on 'RegID', 'Configuration' and 'Value'. Several 'Configuration's can contain a 'RegID', and only one 'Value' exists for a given 'Configuration'+'RegID' combination.
I would like to query a table that show rows of 'RegID' and columns of 'Configuration' listing 'Value' for each 'RegID' and 'Configuration', if any.
I have tried to solve it with the following query, but it just feels wrong to add all configurations manually. There can be up to 9 configurations.
SELECT DISTINCT sp.RegID,
(
SELECT sp1.Value
FROM StandardParameters sp1
WHERE sp.RegID = sp1.RegID
AND sp1.Configuration = 'conf1'
) AS Conf1,
(
SELECT sp1.Value
FROM StandardParameters sp1
WHERE sp.RegID = sp1.RegID
AND sp1.Configuration = 'conf2'
) as Conf2
FROM StandardParameters sp
Any idea on how to optimize this so I don't have to manually list all the configurations? I have the feeling I am missing something obvious. May be related to group-by, but I can't figure out how to get the grouped values as columns. :-)
Sample Data
RegID Configuration Value
reg1 conf1 1
reg2 conf1 2
reg1 conf2 3
reg3 conf2 4
Desired Output
conf1 conf2
reg1 1 3
reg2 2 NULL
reg3 NULL 4