I am using SQL Server 2012 for my database. Now, I have one table with following details.
ID COLUMN1 COLUMN2
1 A B
Now i want result like this.
ID Values
1 A
1 B
Can any one suggest me how to do? I know i can do using pivot or unpivot. but i dont know how to do? and is there other way to do this?
Please help me out to write query for the same.
Thanks in advance.
atable ) SELECT Name, [Value+] = [1], [Value-] = [2] FROM ranked PIVOT (MIN(Value) FOR RN IN ([1], [2])) AS p ; As you can see it gives the ranking of 1 to each row with the lower Sequence per Name , and ranking of 2 to that with the higher Sequence .
You may use the IN, ANY, or ALL operator in outer query to handle a subquery that returns multiple rows. Contents: Using IN operator with a Multiple Row Subquery. Using NOT IN operator with a Multiple Row Subquery.
You can concatenate rows into single string using COALESCE method. This COALESCE method can be used in SQL Server version 2008 and higher. All you have to do is, declare a varchar variable and inside the coalesce, concat the variable with comma and the column, then assign the COALESCE to the variable.
You can use UNPIVOT to get the final result:
select id, value
from yourtable
unpivot
(
value
for col in (COLUMN1, COLUMN2)
) u;
See SQL Fiddle with Demo
Or you can use CROSS APPLY to get it:
select id, value
from yourtable
cross apply
(
values ('COLUMN1', COLUMN1), ('COLUMN2', COLUMN2)
) c (col, value)
See SQL Fiddle with Demo
select id, col1 as value
from your_table
union all
select id, col2 as value
from your_table
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With