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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:10:19+00:00 2026-06-03T01:10:19+00:00

I am trying to get a handle on manipulating table data into more visually

  • 0

I am trying to get a handle on manipulating table data into more visually appealing formats for output. This could be part of the problem as what I want may be intended for separate reporting software.

I have a table that looks like this

teacher    student
----------------------
teacher1   Bob
teacher1   Jim
teacher2   Sam
teacher3   Bill
teacher3   John
teacher3   Eric

I want a table that looks something like this:

teacher1    teacher2    teacher3
---------------------------------
Bob         Sam          Bill
Jim         null         John
null        null         Eric

So I tried stuffing all the teacher names in a variable and then using a Pivot but since I have to choose an aggregate I can only get the Max or Min student like this:

DECLARE @teacherList AS VARCHAR(max)

SELECT @teacherList = Stuff((SELECT DISTINCT',[' + teacher + ']'
                              FROM myTable
                              FOR xml path('')), 1, 1, '')

DECLARE @dynamic_pivot_query AS VARCHAR(max)

SET @dynamic_pivot_query = 'select' + @teacherList + 
'from 
(
    SELECT [teacher],[student]
    FROM [dbo].[myTable]
) as S
Pivot
(
    MIN([student])
    FOR teacher IN (' + @teacherList + ')
) as P
'
EXEC(@dynamic_pivot_query)  

The result of this is:

teacher1    teacher2    teacher3
---------------------------------
Bob         Sam          Bill

Assuming the following:

  1. # of teachers and their names are unknown (variable)
  2. # of students per teacher is unknown and likely different for every teacher

Is there a way to do this?

  • 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-03T01:10:21+00:00Added an answer on June 3, 2026 at 1:10 am

    You can use row_number to get the result you want.

    SET @dynamic_pivot_query = 'select ' + @teacherList + 
    'from 
    (
        SELECT [teacher],[student], row_number() over(partition by teacher order by student) as rn
        FROM [dbo].[myTable]
    ) as S
    Pivot
    (
        MIN([student])
        FOR teacher IN (' + @teacherList + ')
    ) as P
    '
    

    Update:
    To remove the SQL Injection vulnerability you should use quotename to properly quote your field list.

    SELECT @teacherList = Stuff((SELECT DISTINCT',' + quotename(teacher)
                                  FROM myTable
                                  FOR xml path('')), 1, 1, '') 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to get a handle on the FTP library in Python. :) Got this
I'm trying to get a handle on this whole Ant buildsystem thing. I've created
Is there any documentation on this? I'm trying to get a handle on the
I'm trying to get a better handle on using sockets asynchronously. According to this
I am trying to get the handle of a window from a process. Now,
I'm trying to get a handle on whether we have a problem in our
I am trying to get a handle on Solrnet and interacting an ASP.NET site
I'm trying to get a handle to the foreground window in C#/Net 2.0/WinForms by
I am trying to get a list of receivers that handle android.intent.action.BOOT_COMPLETED for other
I am trying to get a handle on the state of the art regarding

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.