0

i have a table with schema like below image

             id     empcode      question      answer     date
             1        011        ur name?      abcd         20/10/2013
             2        011        ur lastname?  abcd         20/10/2013
             3        011        ur number?    abcd         20/10/2013
             4        011        ur email?     abcd         20/10/2013
             5        011        ur name?      xyz          21/10/2013
             6        011        ur lastname?  xyz          21/10/2013
             7        011        ur number?    xyz          21/10/2013
             8        011        ur email?     xyz          21/10/2013

i need select query table like

        date    empcode       ur name?     ur lastname?      ur number?  ur email? 
     20/10/2013       011           abcd          abcd             abcd        abcd 
    21/10/2013        011            xyz           xyz             xyz        xyz  
Barmar
  • 741,623
  • 53
  • 500
  • 612
Ranvijay Singh
  • 356
  • 1
  • 2
  • 9
  • 4
    Search for "SQL Server PIVOT" on this site - there's gotta be **thousands** of questions (and answers!) for this already .... – marc_s Nov 23 '13 at 08:14
  • thanks for this term i will read it later, but right now i want exact solution. – Ranvijay Singh Nov 23 '13 at 08:29
  • got my solution SELECT * FROM ( SELECT [question_id], [date], [answer] FROM dsr_ques_ans where empcode='EMP-820485-2' ) AS source PIVOT ( MAX([answer]) FOR [question_id] IN ([1], [2], [3],[4]) ) as pvt; SELECT date, MAX(CASE WHEN question_id= '1' THEN answer ELSE NULL END) [1], MAX(CASE WHEN question_id= '2' THEN answer ELSE NULL END) [2], MAX(CASE WHEN question_id= '3' THEN answer ELSE NULL END) [3], MAX(CASE WHEN question_id= '4' THEN answer ELSE NULL END) [4] FROM dsr_ques_ans GROUP BY date – Ranvijay Singh Nov 23 '13 at 09:55

0 Answers0