Click here to Skip to main content
15,867,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

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


got my solution
 
Share this answer
 
Comments
thatraja 26-Nov-13 4:34am    
Sorry I missed your comment during weekend, Glad you have posted solution, 5!
Use PIVOT - UNPIVOT

Using PIVOT and UNPIVOT[^]
Pivoting data in SQL Server[^](Alternate ways)
 
Share this answer
 
Comments
[no name] 23-Nov-13 3:35am    
yes it is but how can i implement this. i need exact solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900