Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8843931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:21:34+00:00 2026-06-14T11:21:34+00:00

I have scenario like User | Session | Response u1 | s1 | r1

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:21:35+00:00Added an answer on June 14, 2026 at 11:21 am

    You can implement the PIVOT function to perform this transformation of data. If you have a known number of values, then you can hard-code the query:

    select *
    from 
    (
      select [user], session, response, 
        'response'+ cast(row_number() over(partition by [user], session order by response) as varchar(10)) rn
      from yourtable
    ) src
    pivot
    (
      max(response)
      for rn in ([response1], [response2])
    ) piv
    

    See SQL Fiddle with Demo

    But if you have an unknown number of responses, then you will want to use dynamic sql:

    DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    select @cols = STUFF((SELECT distinct ',' + QUOTENAME('response'+ cast(row_number() over(partition by [user], session order by response) as varchar(10))) 
                        from yourtable
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    set @query = 'SELECT [user], session, ' + @cols + ' from 
                 (
                  select [user], session, response, 
                    ''response''+ cast(row_number() over(partition by [user], session order by response) as varchar(10)) rn
                  from yourtable
                ) x
                pivot 
                (
                    max(response)
                    for rn in (' + @cols + ')
                ) p '
    
    execute(@query)
    

    See SQL Fiddle with Demo

    Both will produce the same result:

    | USER | SESSION | RESPONSE1 | RESPONSE2 |
    ------------------------------------------
    |   u1 |      s1 |        r1 |        r2 |
    |   u1 |      s2 |        r3 |    (null) |
    |   u2 |      s2 |        r4 |        r5 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a scenario outline table that looks like the following: Scenario Outline: Verify
I will like to know: I have a scenario. If a user adds a
I have a scenario that looks like this: #include <algorithm> using namespace std; //
We have a scenario in which we like to detect when the user has
I have a scenario like this: I need to pass @id to the stored
I have a scenario like the below.. <h:selectOneRadio id=someId value=#{myBean.type} required=true> <f:ajax event=valueChange execute=@form
I have a scenario like this: form = MockRepository.GenerateMock<IAddAddressForm>(); mediator = new AddAddressMediator(form); The
I have this scenario where I would like to redirect my domains using the
I have one scenario where I need to select all files having aliencoders.numeric-digits like
I have an architecture scenario and I would like to discuss to get your

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.