I have a need to convert data in a single column into a table. The issue is that the number of columns in my table will be dynamic.
So I have data which is for example as below:
formid
column
row
Value
001
1
1
Blue
001
2
1
Audi
002
1
1
Red
002
2
1
Audi
001
1
2
Black
001
2
2
Merc
I then need to join on the formid and row so my output would be:
001 | Blue | Audi
001 | Black | Merc
002 | Red | Audi
This is relatively easy to do if i always know there are a set number of columns. As I cycle through different tables however the number of columns changes.
I have tried a pivot query but this always needs some form of aggregation or max/min so only ever returns one row per formid.
I'm not actually sure this can be done so any help is appreciated.
Thanks for this jotorre but i'm not sure it answers my query unfortunately.
It still seems to need me to declare how many columns I have, the point is that I do not know this, there were two in my example but there could be any number.
The result is also incorrect as row 4 is a Black Audi which doesn't exist in my example data.
select formid
,max(case when [column]=1 then [value] else null end) as colour
,max(case when [column]=2 then [value] else null end) as brand
from yourtable
group by formid
,[row]
order by formid
;
Ps.: I haven't tested this, as you didn't provide consumable data, so there might be a "syntax feature" to correct.
Is there a maximum number of possible values? How are you determining which column data to display - is it all available or will there be some input parameter?
Pick a maximum number of columns to be returned - code for that max number. If there isn't data available - those values will be NULL.