I'm using SQL Server. I have the following table (MyTable
):
ID (int)
value (varchar)
I have the following query:
select distinct value
from MyTable
And the output is:
Value_1
Value_2
Value_3
...
...
...
Value_450
I would like to crate the following table (when value = 'value_1' then 1 else o...):
ID | Value_1 | Value_2 | Value_3 | Value_4 | .... | Value_450
1 | 0 | 1 | 0 | 0 | .... | 0
2 | 0 | 0 | 0 | 1 | .... | 0
3 | 1 | 0 | 0 | 0 | .... | 0
4 | 0 | 1 | 0 | 0 | .... | 0
5 | 0 | 0 | 0 | 0 | .... | 1
6 | 1 | 0 | 0 | 0 | .... | 0
7 | 0 | 0 | 0 | 1 | .... | 0
8 | 0 | 0 | 0 | 0 | .... | 1
If the distinct values of value
was small, I would use Case
statement for such as query. What should I do in this case which I have so many values? Any smart way to do so?