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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:48:56+00:00 2026-05-22T16:48:56+00:00

I do not know how many courses each person will have or what the

  • 0

I do not know how many courses each person will have or what the names of the courses are due to the number of possibilities so I can’t use a normal sub-select or case.

I have a table like this:

ID    NAME     CLASS
----------------------
1     BOB      Course1
1     BOB      Course2
1     BOB      Course3
2     JOHN     Course1
2     JOHN     Course2
3     SAM      Course1
4     BILL     Course2
4     BILL     Course3

I want to make it look like this where the last column is for whoever has the most classes:

ID    NAME     CLASS     CLASS2     CLASS3     CLASS4..........
------------------------------------------------------
1     BOB      Course1   Course2    Course3    NULL............
2     JOHN     Course1   Course2    NULL       NULL............
3     SAM      Course1   NULL       NULL       NULL............
4     BILL     Course2   Couse3     NULL       NULL............

The best idea I had was to keep getting the max CLASS and deleting it until I ran out of records.

edit: To Clarify: My column headers will not be actual courses just a way to number how many courses a teacher is assigned to.

  • 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-22T16:48:57+00:00Added an answer on May 22, 2026 at 4:48 pm

    Abe’s suggestion looks like this:

     select * from Class
     pivot (COUNT(ID) for CLASS in ([Course1],[Course2],[Course3])) as Taking
    

    Which gives results like this:

    Name    Course1 Course2 Course3
    BILL    0       1       1
    BOB     1       1       0       
    JOHN    1       1       0
    SAM     1       0       0       
    

    Of course this is slightly different than what you want… you want course 1 to have a value for the “first course.” The only way to do this is with dynamic SQL.

    If you want a comma separated list of courses (in one column) that is much easier and faster to generate.


    Comma list

    select c.Name, 
           STUFF((SELECT ( ', ' + c2.Class )
                   FROM #Class c2
                   WHERE c.Name = c2.Name
                   ORDER BY c2.Class 
                   FOR XML PATH( '' )),1,2,'') as [Class List]
    FROM #Class c 
    GROUP BY c.Name 
    

    Gives you:

    Name   Class List
    BILL   Course2, Course3
    BOB    Course1, Course2, Course3
    JOHN   Course1, Course2
    SAM    Course1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know many people who use computers every day, who do not know how
I have a complex application with many pages. Each page can have many possible
I'm not sure if many people know about this text-editor? jEdit was kinda big
I know that there are many free and not so free compression libraries out
I know this has been discussed many times, but I am not sure I
I faced a little trouble - I do not know if I can define
I have a user table in postgress. Each user can have from 0 to
I know it's a good practice to minimize the number of requests each page
There are many users, many groups that users can be part of. Each group
If you do not know what Pipe Viewer is (I did not know about

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.