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

  • SEARCH
  • Home
  • 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 3346348
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:17:36+00:00 2026-05-18T01:17:36+00:00

I had the following physical table of survey application result. It’s not relation sort

  • 0

I had the following physical table of “survey application” result. It’s not relation sort of speak the way storing the result.

As you can see the [84273x1x1] and [84273x1x2] are dropdownlist question which returning code while [84273x1x4] is a free text.

UserID; UserName; Email; [84273x1x1]; [84273x1x2]; [84273x1x4]; [84273x2x5]; [84273x2x6]; [84273x2x7];
1; "Name1"; "name1@email.com"; "A101", "A203", "Test answer bla bla"; "A102", "A201", "Test answer bla bla"
2; "Name2"; "name2@email.com"; "A102", "A202", "This is my comment"; "A101", "A203", "This is my comment";

Something that I found that:
[84273x1x1] is corresponding to:

84273 = SurveyID
1 = PageID
1 = QuestionID

On the Answer table, it has the following:

QID; Code; Answer;
1; A101; 1
1; A102; 2
1; A103; 3
2; A200; 0
2; A201; 1
2; A202; 2
2; A203; 3
5; A101; 1
5; A102; 2
5; A103; 3
6; A200; 0
6; A201; 1
6; A202; 2
6; A203; 3

On the question table:
QID; QuestionType; Title;
1; "DropDownList"; "How do you rate of GROUP-Q1";
2; "DropDownList"; "How do you rate of GROUP-Q1";
3; "Text"; "Comment of Q1";
4; "DropDownList"; "How do you rate of GROUP-Q2";
5; "DropDownList"; "How do you rate of GROUP-Q2";
6; "Text"; "Comment of GROUP-Q2";

The result that I would like to achieve is that pivoting:

UserID; Name; Email; Title; [Question1], [Question2]; [Question3]
1; "Name1"; "name1@email.com"; "GROUP-Q1"; "1"; "3"; "Test answer bla bla"; 
1; "Name1"; "name1@email.com"; "GROUP-Q2"; "2"; "1"; "Test answer bla bla"; 
2; "Name2"; "name2@email.com"; "GROUP-Q1"; "2"; "2"; "Test answer bla bla"; 
2; "Name2"; "name2@email.com"; "GROUP-Q2"; "1"; "3"; "Test answer bla bla"; 

Because this thing needs to be done in TSQL – 2005. When I looked at this, my first thought will be it has to be in Cursor.

Any thought guys?

Thanks

  • 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-05-18T01:17:37+00:00Added an answer on May 18, 2026 at 1:17 am

    How about something like so:

    Select P.UserId, P.Username, P.Email
        , 'GROUP-Q1'
        , A1.Answer As Question1
        , A2.Answer As Question2
        , P.[84273x1x4] As Question3
    From People As P
        Left Join Answers As A1
            On A1.Code = P.[84273x1x1]
                And A1.QID = 1
        Left Join Answers As A2
            On A2.Code = P.[84273x1x2]
                And A2.QID = 2
    Union All
    Select P.UserId, P.Username, P.Email
        , 'GROUP-Q2'
        , A1.Answer As Question1
        , A2.Answer As Question2
        , P.[84273x1x7] As Question3
    From People As P
        Left Join Answers As A1
            On A1.Code = P.[84273x1x5]
                And A1.QID = 5
        Left Join Answers As A2
            On A2.Code = P.[84273x1x6]
                And A2.QID = 6
    

    Here’s another “more” dynamic solution (requires SQL Server 2005+):

    ;With UserRawAnswers As
        (
        Select UserId, 1 As QuestionID, [84273x1x1] As Answer From People
        Union All Select UserId, 2, [84273x1x2] From People
        Union All Select UserId, 3, [84273x1x4] From People
        Union All Select UserId, 5, [84273x1x5] From People
        Union All Select UserId, 6, [84273x1x6] From People
        Union All Select UserId, 6, [84273x1x7] From People
        )
        , UserAnswers As
        (
        Select UA.UserId
            , Right(Q.Title) As Title
            , Coalesce(DropListAnswers.Answer, UA.Answer) As Answer
        From UserRawAnswers As UA
            Join Questions
                On Questions.QID = UA.QID
            Left Join (Answer As DropListAnswers
                Join Questions As DropListQuestions
                    On DropListQuestions.QID = DropListAnswers.QID
                        And DropListQuestions.QuestionType = 'DropDownList')
                On DropListAnswers.Code = UA.Answer
        )
    Select P.UserID, P.Name, P.Email
        , UA.Title
        , Min( Case When UA.QuestionID = 1 Then UA.Answer End ) As Question1
        , Min( Case When UA.QuestionID = 2 Then UA.Answer End ) As Question2
        , Min( Case When UA.QuestionID = 3 Then UA.Answer End ) As Question3
    From UserAnswers As UA
        Join People As P
            On P.UserID = UA.UserId
    Group By P.UserID, P.Name, P.Email, UA.Title
    

    Keep in mind that inherently the SQL language is not designed to deal with dynamic schema (i.e. dynamically generated columns). The only way to build dynamic schema is using dynamic SQL and if you hit that point you might as well do it in the middle tier or a reporting tool. Also, the denormalized structure really makes analysis difficult.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had following cgridview in Yii application, I want to change date format in
If I had the following table. create_table :my_table, :id => false do |t| t.string
I had the following function. It worked, but I don't like the way it
I was doing update of big table in Sybase and had following code (declarations
It is clear. I had following error. How can I remove it. I need
I had the following query that doesn't work and I was wondering how to
I had the following template that essentially changed the color of the border of
I had the following idea: Say we have a webapp written using django which
I had the following problem today, and I was wondering if there is a
I had the following HTML page rendering to use the Google Maps API. This

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.