I have table:
ID Note
1 1 aaa
2 1 bbb
3 1 ccc
4 2 ddd
5 2 eee
6 2 fff
I need to return it as:
ID Note1 Note2 Note3
1 1 aaa bbb ccc
2 2 ddd eee fff
Thank you!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use the
PIVOTfunction for this type of query. If you have a known number of columns, then you can hard-code the values:See SQL Fiddle with Demo
If you are going to have an unknown number of
notesthat you want to turn into columns, then you can use dynamic sql:See SQL Fiddle with Demo
Both will produce the same results.
Or if you do not want to use the
PIVOTfunction, then you can use an aggregate function with aCASEstatement:See SQL Fiddle with Demo