Page 1 of 1

Multiple Rows to One Row (Vertical Pivoting)

Posted: Thu Dec 11, 2008 11:27 am
by rohit_mca2003
Hi,
I have a requirement to pivot the multiple rows into a single row. I have input data as below:

Input Data:
-------------
Id Fname Sname Section Ques Ans
--------------------------------------------------------------------
100 Sam Roy S1 Q11 A11
100 Sam Roy S1 Q12 A12
100 Sam Roy S1 Q13 A13
100 Sam Roy S2 Q21 A21
100 Sam Roy S2 Q22 A22


I like output data as follows in one row and different columns:
-----------------------------------------------------------------------
100 Sam Roy S1 Q11 A11 Q12 A12 Q13 A13 S2 Q21 A21 Q22 A22


Could you please help me to resolve this.

Regards.

Posted: Thu Dec 11, 2008 11:36 am
by Mike
No need to post the same question more than once. This question gets asked a lot here. Try the search function. An exact match on vertical pivot turns up 154 hits.

Mike

Multiple Rows to One Row (Vertical Pivoting)

Posted: Fri Dec 12, 2008 5:40 am
by rohit_mca2003
Thanks for all your replies.
I have searched the topics but I have not got any suitable reply for my input set of data.

If you see the input set it has two pivot columns ID and SECTION. It would be greatful if anyone can provide solution to this example.

Input Data:
-------------
Id Fname Sname Section Ques Ans
--------------------------------------------------------------------
100 Sam Roy S1 Q11 A11
100 Sam Roy S1 Q12 A12
100 Sam Roy S1 Q13 A13
100 Sam Roy S2 Q21 A21
100 Sam Roy S2 Q22 A22


I like output data as follows in one row and different columns:
-----------------------------------------------------------------------
100 Sam Roy S1 Q11 A11 Q12 A12 Q13 A13 S2 Q21 A21 Q22 A22

Posted: Fri Dec 12, 2008 12:27 pm
by ray.wurlod
Just generalize to two pivot keys from the solutions given. For example use additional stage variables.