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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:06:50+00:00 2026-05-22T19:06:50+00:00

I have reduced a complex schema to the following samples Students StudentID int, Name

  • 0

I have reduced a complex schema to the following samples

Students

  • StudentID int, Name varchar(50)
  • 1, Bill
  • 2, Amy
  • 3, Beth
  • 4, Scott
  • 5, Steve

Classes

  • ClassID int, Name varchar(50), Period varchar(50)
  • 1, Algebra, Period1
  • 2, Geography, Period3
  • 3, Biology, Period5
  • 4, Physics, Period4
  • 5, Speech, Period2
  • 6, History, Period6

and a junction table

StudentsClasses

  • StudentID int, ClassID int
  • 1, 1
  • 1, 4
  • 1, 5
  • 2, 6
  • 3, 5
  • 3, 4
  • 3, 6
  • 4, 1
  • 4, 4
  • 5, 5
  • 5, 6

My goal is to list each student with their selected classes listed in period order. I have the following select

SELECT Name,Period1, Period2, Period3, 
    Period4, Period5, Period6  
    FROM (
SELECT _Students.Name AS [NAME],_Classes.Period AS PIVOT_CODE, _Classes.name as [Class] 
FROM _Classes 
    INNER JOIN _StudentsClasses ON _Classes.ClassID=_StudentsClasses.ClassID 
    INNER JOIN _Students ON _StudentsClasses.StudentID=_Students.StudentID
)
    AS data
    PIVOT 
    (  min([Class])  FOR [PIVOT_CODE] IN 
        (Period1, Period2, Period3, 
    Period4, Period5, Period6)
    ) AS pvt

Which results in

Name   Period1   Period2   Period3   Period4   Period5   Period6
------ --------- --------- --------- --------- --------- ----------
Amy    NULL      NULL      NULL      NULL      NULL      History
Beth   NULL      Speech    NULL      Physics   NULL      History
Bill   Algebra   Speech    NULL      Physics   NULL      NULL
Scott  Algebra   NULL      NULL      Physics   NULL      NULL
Steve  NULL      Speech    NULL      NULL      NULL      History

Where I need help is I need is to move all non-nulls towards the left column so there are no blanks. The column names can be renamed, for example

Name   Choice1   Choice2   Choice3   Choice4   Choice5   Choice6
------ --------- --------- --------- --------- --------- ----------
Amy    History
Beth   Speech    Physics   History
Bill   Algebra   Speech    Physics
Scott  Algebra   Physics
Steve  Speech    History

I can do this by selecting the pivot into a temp table then iterating over each row/column with a cursor but I’d like to avoid that. Any suggestions are greatly appreciated.

  • 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-22T19:06:50+00:00Added an answer on May 22, 2026 at 7:06 pm

    Assuming SQL Server 2005 (at least), using ROW_NUMBER() to order the choices:

    SELECT Name, Choice1, Choice2, Choice3, Choice4, Choice5, Choice6  
    FROM (
        SELECT 
            S.Name AS [NAME], 
            'Choice' + CAST(ROW_NUMBER() OVER(PARTITION BY S.Name ORDER BY S.Name, C.Period) AS VARCHAR) AS PIVOT_CODE, 
             C.Name as [Class]
        FROM Classes C
            JOIN StudentsClasses SC ON C.ClassID = SC.ClassID 
            JOIN Students S ON SC.StudentID = S.StudentID
        )
        AS data
        PIVOT 
        (  min([Class])  FOR [PIVOT_CODE] IN 
            (Choice1, Choice2, Choice3, Choice4, Choice5, Choice6)
        ) AS pvt
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have you managed to get Aptana Studio debugging to work? I tried following this,
I have a rather complex ListView, with variable list item heights. Under certain conditions,
I have a hash table where the keys are rather complex lists, with sublists
Let say I have the following desire, to simplify the IConvertible's to allow me
How can I reduce resources inside my application? I have tried a number of
Have just started using Google Chrome , and noticed in parts of our site,
Have you ever seen any of there error messages? -- SQL Server 2000 Could
Have you guys had any experiences (positive or negative) by placing your source code/solution
Have just started using Visual Studio Professional's built-in unit testing features, which as I
Have you used VS.NET Architect Edition's Application and System diagrams to start designing a

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.