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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:40:02+00:00 2026-06-11T07:40:02+00:00

I’m having a problem with Crosstab query in SQL Server. Suppose that I have

  • 0

I’m having a problem with Crosstab query in SQL Server.

Suppose that I have data as below:

| ScoreID | StudentID |      Name |    Sex | SubjectName | Score |
------------------------------------------------------------------
|       1 |         1 | Student A |   Male |           C |   100 |
|       2 |         1 | Student A |   Male |         C++ |    40 |
|       3 |         1 | Student A |   Male |     English |    60 |
|       4 |         1 | Student A |   Male |    Database |    15 |
|       5 |         1 | Student A |   Male |        Math |    50 |
|       6 |         2 | Student B |   Male |           C |    77 |
|       7 |         2 | Student B |   Male |         C++ |    12 |
|       8 |         2 | Student B |   Male |     English |    56 |
|       9 |         2 | Student B |   Male |    Database |    34 |
|      10 |         2 | Student B |   Male |        Math |    76 |
|      11 |         3 | Student C | Female |           C |    24 |
|      12 |         3 | Student C | Female |         C++ |    10 |
|      13 |         3 | Student C | Female |     English |    15 |
|      14 |         3 | Student C | Female |    Database |    40 |
|      15 |         3 | Student C | Female |        Math |    21 |
|      16 |         4 | Student D | Female |           C |    17 |
|      17 |         4 | Student D | Female |         C++ |    34 |
|      18 |         4 | Student D | Female |     English |    24 |
|      19 |         4 | Student D | Female |    Database |    56 |
|      20 |         4 | Student D | Female |        Math |    43 |

I want to make query which show the result as below:

| StuID| Name      | Sex    | C  | C++ | Eng | DB | Math | Total | Average |
|  1   | Student A | Male   | 100|  40 | 60  | 15 |  50  |  265  |   54    |
|  2   | Student B | Male   | 77 |  12 | 56  | 34 |  76  |  255  |   51    |
|  3   | Student C | Female | 24 |  10 | 15  | 40 |  21  |  110  |   22    |
|  4   | Student D | Female | 17 |  34 | 24  | 56 |  43  |  174  |   34.8  |

How could I query to show output like this?

Note:

Subject Name:

  • C
  • C++
  • English
  • Database
  • Math

    will be changed depend on which subject student learn.

Please go to http://sqlfiddle.com/#!6/2ba07/1 to test this query.

  • 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-11T07:40:05+00:00Added an answer on June 11, 2026 at 7:40 am

    There are two ways to perform a PIVOT static where you hard-code the values and dynamic where the columns are determined when you execute.

    Even though you will want a dynamic version, sometimes it is easier to start with a static PIVOT and then work towards a dynamic one.

    Static Version:

    SELECT studentid, name, sex,[C], [C++], [English], [Database], [Math], total, average
    from 
    (
      select s1.studentid, name, sex, subjectname, score, total, average
      from Score s1
      inner join
      (
        select studentid, sum(score) total, avg(score) average
        from score
        group by studentid
      ) s2
        on s1.studentid = s2.studentid
    ) x
    pivot 
    (
       min(score)
       for subjectname in ([C], [C++], [English], [Database], [Math])
    ) p
    

    See SQL Fiddle with demo

    Now, if you do not know the values that will be transformed then you can use Dynamic SQL for this:

    DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    select @cols = STUFF((SELECT distinct ',' + QUOTENAME(SubjectName) 
                        from Score
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    
    
    set @query = 'SELECT studentid, name, sex,' + @cols + ', total, average
                  from 
                 (
                    select s1.studentid, name, sex, subjectname, score, total, average
                    from Score s1
                    inner join
                    (
                      select studentid, sum(score) total, avg(score) average
                      from score
                      group by studentid
                    ) s2
                      on s1.studentid = s2.studentid
                ) x
                pivot 
                (
                    min(score)
                    for subjectname in (' + @cols + ')
                ) p '
    
    execute(@query)
    

    See SQL Fiddle with Demo

    Both versions will yield the same results.

    Just to round out the answer, if you do not have a PIVOT function, then you can get this result using CASE and an aggregate function:

    select s1.studentid, name, sex, 
      min(case when subjectname = 'C' then score end) C,
      min(case when subjectname = 'C++' then score end) [C++],
      min(case when subjectname = 'English' then score end) English,
      min(case when subjectname = 'Database' then score end) [Database],
      min(case when subjectname = 'Math' then score end) Math,
      total, average
    from Score s1
    inner join
    (
      select studentid, sum(score) total, avg(score) average
      from score
      group by studentid
    ) s2
      on s1.studentid = s2.studentid
    group by s1.studentid, name, sex, total, average
    

    See SQL Fiddle with Demo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

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.