I have scenario like
User | Session | Response
u1 | s1 | r1
u1 | s1 | r2
u1 | s2 | r3
u2 | s2 | r4
u2 | s2 | r5
AND the Output needs to be Like
OUT PUT Table
User | Session | response1 | response2 | Session.....n
u1 | s1 | r1 | r2
u1 | s2 | r3 | NULL
u2 | s2 | r4 | r5
That Session column may be n time depending upon number responses.
You can implement the
PIVOTfunction to perform this transformation of data. If you have a known number of values, then you can hard-code the query:See SQL Fiddle with Demo
But if you have an unknown number of responses, then you will want to use dynamic sql:
See SQL Fiddle with Demo
Both will produce the same result: