Sachin25
Sachin25

Reputation: 17

converting rows into columns

I have a table in sql server 2005 where there are 3 columns id ,question and answer.

the data is as below-

id  question  answer    
1   married    yes
1   name       John
2   married    No
2   name       Dave
3   married    yes
3   name       Rob

I'm expecting results like the one below-

ID  married  name
1   yes      John
2   no       Dave
3   yes      Rob

Thanks in advance

Upvotes: 0

Views: 31

Answers (1)

M.Ali
M.Ali

Reputation: 69574

Select ID
      ,MAX(CASE WHEN question = 'married' THEN answer END) AS Married
      ,MAX(CASE WHEN question = 'name'    THEN answer END) AS Name
FROM TableName
GROUP BY ID

Upvotes: 2

Related Questions