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 6885523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:38:43+00:00 2026-05-27T05:38:43+00:00

Using SQL 2005 Here is what my SQL results look like UserID | Frame

  • 0

Using SQL 2005
Here is what my SQL results look like

UserID | Frame | Choice | Description | DateTime
------------------------------------------------
bcn005 | PL_P03| 1      | Rules-Based | 2011-10-24 14:14:26
bcn005 | PL_P04| 0      | VirtueBased | 2011-10-24 14:16:37
...
bmk172 | Prac_1| 0      | None        | 2011-10-25 12:45:38
...

My client wants the results to look like this

UserID | Frame | Choice | Description | DateTime  | Frame | Choice | Description | DateTime 
-------------------------------------------------------------------------------------------
bcn005 | PL_P03| 1      | Rules-Based | 2011-10-..| PL_P04| 0      | VirtueBased | 2011-10-..
bmk172 | Prac_1| 0      | None        | 2011-10-..|

Where all of a participants responses are on one row. There basically needs to be another set of Frame/Choice Description/DateTime for every row of the result set.

These are survey results, but it is the type of survey where one participant might have went back and changed their answer 5 times. So the amount of Frame/Choice Description/DateTime for each UserID will be different.

I know I should use a stored procedure (or maybe even an excel macro) but I am completely new to both of these. Any guidance is appreciated. Here is my SQL query

select UserID, locationName AS 'Frame', student_response AS 'Choice',
description, DateTime
from tblStudentResponses
WHERE GroupID in (18,20,21,36,37,38,39,40)
AND UserID in (SELECT UserID FROM tblStudentResponses WHERE
locationName = 'Character1Certificate')
AND DateTime > '2011-10-01'
AND type = 'choice'
order by UserID

!! UPDATE !! Full working solution and test code


–Sample Data Preperation
DECLARE @FrameChoices as TABLE(UserId varchar(50),Frame varchar(50),Choice varchar(4000),Description varchar(1000),[DateTime] DateTime)

--Test Values
INSERT INTO @FrameChoices(UserId,Frame,Choice,Description,[DateTime])
SELECT  UserId,Frame,Choice,Description,[DateTime]
FROM (
    SELECT 'bcn005' AS UserId, 'PL_P03' AS Frame, '1' AS Choice, 'Rules-Based'
    AS Description, '2011-10-24 14:14:26' AS [DateTime]
    UNION ALL
    SELECT 'bcn005' AS UserId, 'PL_P04' AS Frame, '0' AS Choice, 'VirtueBased'
    AS Description, '2011-10-24 14:16:37' AS [DateTime]
    UNION ALL
    SELECT 'bmk172' AS UserId, 'Prac_1' AS Frame, '0' AS Choice, 'None' AS
    Description, '2011-10-25 12:45:38' AS [DateTime]
) T

— Dynamic SQL Preparation

DECLARE @SQL VARCHAR(MAX)
DECLARE @MaxNumberOfFrames INT

SET @SQL = ''
SET @MaxNumberOfFrames = 1

SELECT @MaxNumberOfFrames = MAX(FrameCount)
  FROM
    (
        SELECT COUNT(1) FrameCount
        FROM @FrameChoices
        GROUP BY UserId  
    ) T

 --needed to be part of the SQL string to EXEC at the bottom
SET @SQL = 'DECLARE @FrameChoices as TABLE(UserId varchar(50),Frame varchar(50),Choice varchar(4000),Description varchar(1000),[DateTime] DateTime)' 

SET @SQL = @SQL + ' INSERT INTO @FrameChoices(UserId,Frame,Choice,Description,[DateTime])'
SET @SQL = @SQL + ' SELECT  UserId,Frame,Choice,Description,[DateTime]'
SET @SQL = @SQL + ' FROM ('
SET @SQL = @SQL + '     SELECT ''bcn005'' AS UserId, ''PL_P03'' AS Frame, ''1'' AS Choice, ''Rules-Based'''
SET @SQL = @SQL + '     AS Description, ''2011-10-24 14:14:26'' AS [DateTime]'
SET @SQL = @SQL + '     UNION ALL'
SET @SQL = @SQL + '     SELECT ''bcn005'' AS UserId, ''PL_P04'' AS Frame, ''0'' AS Choice, ''VirtueBased'''
SET @SQL = @SQL + '     AS Description, ''2011-10-24 14:16:37'' AS [DateTime]'
SET @SQL = @SQL + '     UNION ALL'
SET @SQL = @SQL + '     SELECT ''bmk172'' AS UserId, ''Prac_1'' AS Frame, ''0'' AS Choice, ''None'' AS'
SET @SQL = @SQL + '     Description, ''2011-10-25 12:45:38'' AS [DateTime]'
SET @SQL = @SQL + ' ) T'

SET @SQL = @SQL + ' SELECT UserId'

--changed size to max
DECLARE @ColumnSQL VARCHAR(MAX)
DECLARE @CurrentFrame INT

SET @CurrentFrame = 1

WHILE (@CurrentFrame <= @MaxNumberOfFrames)
BEGIN
    SET @ColumnSQL = ', MAX(CASE WHEN RowNumber ='+CAST(@CurrentFrame AS VARCHAR)+ ' THEN Frame ELSE '''' END) AS Frame_'+CAST(@CurrentFrame AS VARCHAR)
                    + ', MAX(CASE WHEN RowNumber ='+CAST(@CurrentFrame AS VARCHAR)+ ' THEN Choice ELSE '''' END) AS Choice_'+CAST(@CurrentFrame AS VARCHAR)
                    + ', MAX(CASE WHEN RowNumber ='+CAST(@CurrentFrame AS VARCHAR)+ ' THEN Description ELSE '''' END) AS Description_'+CAST(@CurrentFrame AS VARCHAR)
                    + ', MAX(CASE WHEN RowNumber ='+CAST(@CurrentFrame AS VARCHAR)+ ' THEN [DateTime] ELSE NULL END) AS DateTime_'+CAST(@CurrentFrame AS VARCHAR)
    SET @SQL = @SQL + @ColumnSQL
    SET @CurrentFrame = @CurrentFrame + 1
END

SET @SQL = @SQL + ' FROM ( SELECT UserId,Frame,Choice,Description,[DateTime], ROW_NUMBER() OVER (PARTITION BY UserId ORDER BY [DateTime]) AS RowNumber FROM @FrameChoices) T GROUP BY T.UserId'

PRINT @SQL

— This generated SQL then can be executed by

EXEC(@SQL)
 -- OR
EXEC SP_EXECUTESQL(@SQL)

— Sample SQL generated from above query

DECLARE @FrameChoices as TABLE(UserId varchar(50),Frame varchar(50),Choice varchar(4000),Description varchar(1000),[DateTime] DateTime) INSERT INTO @FrameChoices(UserId,Frame,Choice,Description,[DateTime]) SELECT  UserId,Frame,Choice,Description,[DateTime] FROM (     SELECT 'bcn005' AS UserId, 'PL_P03' AS Frame, '1' AS Choice, 'Rules-Based'  AS Description, '2011-10-24 14:14:26' AS [DateTime]     UNION ALL   SELECT 'bcn005' AS UserId, 'PL_P04' AS Frame, '0' AS Choice, 'VirtueBased'  AS Description, '2011-10-24 14:16:37' AS [DateTime]     UNION ALL   SELECT 'bmk172' AS UserId, 'Prac_1' AS Frame, '0' AS Choice, 'None' AS  Description, '2011-10-25 12:45:38' AS [DateTime] ) T SELECT UserId, MAX(CASE WHEN RowNumber =1 THEN Frame ELSE '' END) AS Frame_1, MAX(CASE WHEN RowNumber =1 THEN Choice ELSE '' END) AS Choice_1, MAX(CASE WHEN RowNumber =1 THEN Description ELSE '' END) AS Description_1, MAX(CASE WHEN RowNumber =1 THEN [DateTime] ELSE NULL END) AS DateTime_1, MAX(CASE WHEN RowNumber =2 THEN Frame ELSE '' END) AS Frame_2, MAX(CASE WHEN RowNumber =2 THEN Choice ELSE '' END) AS Choice_2, MAX(CASE WHEN RowNumber =2 THEN Description ELSE '' END) AS Description_2, MAX(CASE WHEN RowNumber =2 THEN [DateTime] ELSE NULL END) AS DateTime_2 FROM ( SELECT UserId,Frame,Choice,Description,[DateTime], ROW_NUMBER() OVER (PARTITION BY UserId ORDER BY [DateTime]) AS RowNumber FROM @FrameChoices) T GROUP BY T.UserId
  • 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-27T05:38:44+00:00Added an answer on May 27, 2026 at 5:38 am

    If SQL 2005 or ABove you can try pivot.

    If SQL 2000, then use combination of Group By and Case.

    SELECT GC1,
           GC2,
           MAX(CASE WHEN CONDITION1 THEN Value1 ELSE Value2 END) AS DerivedColumn
    FROM Table1
    GROUP BY GC1, GC2
    

    References:
    SQL Server PIVOT and UNPIVOT

    Pivot with Dynamic Column

    Stack Overflow Pivot Question

    [Solution]

    — Sample Data Preparation

    DECLARE @FrameChoices TABLE
    (
      UserId varchar(20),
      Frame varchar(20),
      Choice int,
      Description varchar(20),
      [DateTime] DateTime
    )
    
    INSERT INTO @FrameChoices(UserId,Frame,Choice,Description,[DateTime])
    SELECT  UserId,Frame,Choice,Description,[DateTime]
      FROM (
            SELECT 'bcn005' AS UserId, 'PL_P03' AS Frame, 1 AS Choice, 'Rules-Based' AS Description, '2011-10-24 14:14:26' AS [DateTime]
            UNION ALL
            SELECT 'bcn005' AS UserId, 'PL_P04' AS Frame, 0 AS Choice, 'VirtueBased' AS Description, '2011-10-24 14:16:37' AS [DateTime]
            UNION ALL
            SELECT 'bmk172' AS UserId, 'Prac_1' AS Frame, 0 AS Choice, 'None' AS Description, '2011-10-25 12:45:38' AS [DateTime]
           ) T
    

    — Dynamic SQL Preparation

    DECLARE @SQL VARCHAR(4000)
    DECLARE @MaxNumberOfFrames INT
    
    SET @SQL = ''
    SET @MaxNumberOfFrames = 1
    
    SELECT @MaxNumberOfFrames = MAX(FrameCount)
      FROM
    (
    SELECT COUNT(1)FrameCount
      FROM @FrameChoices
     GROUP BY UserId  
    ) T
    
    SET @SQL = 'SELECT UserId'
    
    DECLARE @ColumnSQL VARCHAR(512)
    DECLARE @CurrentFrame INT
    
    SET @CurrentFrame = 1
    
    WHILE (@CurrentFrame <= @MaxNumberOfFrames)
    BEGIN
        SET @ColumnSQL = ', MAX(CASE WHEN RowNumber ='+CAST(@CurrentFrame AS VARCHAR)+ ' THEN Frame ELSE NULL END) AS Frame_'+CAST(@CurrentFrame AS VARCHAR)
                        + ', MAX(CASE WHEN RowNumber ='+CAST(@CurrentFrame AS VARCHAR)+ ' THEN Choice ELSE NULL END) AS Choice_'+CAST(@CurrentFrame AS VARCHAR)
                        + ', MAX(CASE WHEN RowNumber ='+CAST(@CurrentFrame AS VARCHAR)+ ' THEN Description ELSE NULL END) AS Description_'+CAST(@CurrentFrame AS VARCHAR)
                        + ', MAX(CASE WHEN RowNumber ='+CAST(@CurrentFrame AS VARCHAR)+ ' THEN [DateTime] ELSE NULL END) AS DateTime_'+CAST(@CurrentFrame AS VARCHAR)
        SET @SQL = @SQL + @ColumnSQL
        SET @CurrentFrame = @CurrentFrame + 1
    END
    
    SET @SQL = @SQL + ' FROM ( SELECT UserId,Frame,Choice,Description,[DateTime], ROW_NUMBER() OVER (PARTITION BY UserId ORDER BY [DateTime]) AS RowNumber FROM @FrameChoices) T GROUP BY T.UserId'
    
    PRINT @SQL
    

    — This generated SQL then can be executed by

    EXEC(@SQL)
     -- OR
    EXEC SP_EXECUTESQL(@SQL)
    

    — Sample SQL generated from above query

    SELECT UserId, MAX(CASE WHEN RowNumber =1 THEN Frame ELSE NULL END) AS Frame_1, MAX(CASE WHEN RowNumber =1 THEN Choice ELSE NULL END) AS Choice_1, MAX(CASE WHEN RowNumber =1 THEN Description ELSE NULL END) AS Description_1, MAX(CASE WHEN RowNumber =1 THEN [DateTime] ELSE NULL END) AS DateTime_1, MAX(CASE WHEN RowNumber =2 THEN Frame ELSE NULL END) AS Frame_2, MAX(CASE WHEN RowNumber =2 THEN Choice ELSE NULL END) AS Choice_2, MAX(CASE WHEN RowNumber =2 THEN Description ELSE NULL END) AS Description_2, MAX(CASE WHEN RowNumber =2 THEN [DateTime] ELSE NULL END) AS DateTime_2 FROM ( SELECT UserId,Frame,Choice,Description,[DateTime], ROW_NUMBER() OVER (PARTITION BY UserId ORDER BY [DateTime]) AS RowNumber FROM @FrameChoices) T GROUP BY T.UserId
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm running into an odd bug using datetime fields in SQL Server 2005. The
I am using MS SQL Server 2005. I think PIVOT could help me here,
Using SQL 2005: Taking too much time to execute I want to filter the
We are using SQL 2005 and the try-catch functionality to handle all of our
I've got a query that returns a proper result set, using SQL 2005. It
Using SQL Server 2005 and Management Studio how do I insert a picture into
[using SQL Server 2005] I have a table full of users, I want to
Using SQL Server 2005 Database1 Table1 ID Date Status 001 23-02-2009 Worked 001 24-02-2009
Using SQL Server 2005 I want to filter the column where column value not
Using SQL Server 2005, how can I authenticate a username/password pair against a non-AD

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.