I’m looking to ‘flatten’ my dataset in order to facilitate data mining.
Each categorical column should be changed to multiple Boolean columns.
I have a column with categorical values, e.g.:
ID col1
1 A
2 B
3 A
I’m looking for a way to pivot this table, and have an aggregated function telling me whether this ID has value A or B:
Result:
ID col1A col1B
1 1 0
2 0 1
3 1 0
I tried using PIVOT but have no idea which aggregated function to use inside it.
Also looked for answers in SF but couldn’t find any…
I’m using MS-SQL 2012.
Any help would be appreciated!
Omri
EDIT:
The number of categories in col1 is unknown, therefore the solution must be dynamic.
Thanks 🙂
try this:
IF you have one ID with both A and B and you want to have distinct ID in the output you could do
EDIT
As per your comment, If you do not know the number of options for col1, then you can go for dynamic PIVOT
SQL Fiddle Demo