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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:14:48+00:00 2026-06-17T10:14:48+00:00

I’m trying to create a query with dynamic columns, based on data from three

  • 0

I’m trying to create a query with dynamic columns, based on data from three tables.

This is the database structure:

STUDENT

  • studentID int,
  • studentNumber int,
  • studentName nvarchar(100).

EXAM:

  • examID int,
  • examName varchar(100),
  • examenDate datetime,

EXAM_REGISTRATION:

  • studentID int,
  • examID int,

A record is added to the EXAM_REGISTRATION table when a student has registered for an exam.

What I’m trying to get is a list of all the exams and all the students in a pivot table to see which students have registered for which exams, like this:

enter image description here

Quite frankly I don’t know where to start.

I can query everything individually and put it all together but how can I combine it into one query?

I’ve been researching pivot tables, but every example seems to query only from one table and uses numbers and functions like MIN, AVG etc.

Can someone help me along?

  • 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-17T10:14:50+00:00Added an answer on June 17, 2026 at 10:14 am

    ok lets go

    some data to play with

    create table #student
    (studentID int, studentNumber int, studentName nvarchar(100))
    create table #exam 
    (examID int, examName nvarchar(100), examDate datetime)
    create table #examReg
    (studentID int, examID int)
    
    insert into #student
    values (1, 787878, 'pierwszy')
    ,(2, 89898, 'drugi')
    ,(3, 343434, 'trzeci')
    ,(4, 121212, 'czwarty')
    
    insert into #exam
    values (1, 'exPierwszy', GETDATE())
    ,(2, 'exDrugi', GETDATE())
    ,(3, 'exTrzeci', GETDATE())
    
    insert into #examReg
    values (1,2),(1,3)
    , (2,2),(2,3)
    ,(3,1),(3,2)
    ,(4,1),(4,2),(4,3)
    

    and now the main part, and explanation

    first of all you have to get pivot query

    select examName, examDate , min([1])  , min([2]), min([3]) ,min([4])--studentID as studentID, examDate --,studentNumber
    from 
    (select a.studentID , studentNumber, examDate, examName
    from #student a 
    join #examReg b on a.studentID = b.studentID
    join #exam c on c.examID = b.examID ) as m
    pivot
    (min(studentNumber) FOR studentID in ([1],[2],[3],[4])) as t
    group by examName, examDate
    

    as you have it , just change it select statement and studentID list in pivot declaration, you have to generate those parts dynamicly , so we just copy previously written query and replace columns with our token

        declare @sqlTemplate nvarchar(max) = 
        'select examName, examDate @@sqlColumnList@@
        from 
        (select a.studentID , studentNumber, examDate, examName
        from #student a 
        join #examReg b on a.studentID = b.studentID
        join #exam c on c.examID = b.examID ) as m
        pivot
        (min(studentNumber) FOR studentID in (@@sqlStudentIDList@@)) as t
        group by examName, examDate
    '
    

    after that you generate column list and studentID list by concatenting strings in tsql

    declare @sqlColumnList nvarchar(max) = ''
    select @sqlColumnList += ',min([' + cast(studentID as nvarchar(10)) + ']) as [' + studentName +'(' + cast(studentNumber as nvarchar(10)) + ')]'
    from #student
    declare @sqlStudentIDList nvarchar(max) = ''
    select @sqlStudentIDList += '[' + CAST(studentID as nvarchar(10)) + '],'
    from #student
    
    set @sqlStudentIDList = SUBSTRING(@sqlStudentIDList, 0, LEN(@sqlStudentIDList))
    select @sqlStudentIDList
    

    once you have it , all you have to do is to replace tokens in previous template

    set @sqlTemplate = REPLACE(@sqlTemplate, '@@sqlColumnList@@', @sqlColumnList)
    set @sqlTemplate = REPLACE(@sqlTemplate, '@@sqlStudentIDList@@', @sqlStudentIDList)
    
    
    select @sqlTemplate
    
    exec sp_executesql @sqlTemplate
    

    and thats it
    if you want to read more about pivot go for msdn
    if you want to read about dynamic go for this link

    edit: to adjust the query for the question from comment you would have to change @sqlColumnList like that

    select @sqlColumnList += ',min(' + QUOTENAME(studentID)  + ') as Student' + CAST(studentID as nvarchar(10))  + '_REG,
    '''+ studentName + ''' as Student' + cast(studentID as nvarchar(10)) + '_NAME,
    '+ cast(studentID as nvarchar(10)) + ' as Student' + cast(studentID as nvarchar(10)) + '_ID'
    from #student
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to create an if statement in PHP that prevents a single post
I have a view passing on information from a database: def serve_article(request, id): served_article
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

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.