I have two tables, Answers and Users, which look like this:
Answers (AnswerID, QuestionNumber, Answer, UserID)
Users (UserID, FirstName, LastName)
What SQL query could I run to return a report like this:

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 to transform this data. It seems rather unusual that you have theName(Firstname/Lastname) as the headers but if that is what you want, then you can use the following:See SQL Fiddle with Demo
Result:
I would think you would want the
Question Numbersas the headers with the answers below them like this:See SQL Fiddle with Demo
Result:
The above answers will work great if you have a known number of items to transform. But if you have an unknown number, then you can use dynamic SQL to
PIVOTthe data:See SQL Fiddle with Demo
Result: