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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:20:07+00:00 2026-06-14T09:20:07+00:00

Possible Duplicate: SQL Server dynamic PIVOT query? Is it possible to execute a query

  • 0

Possible Duplicate:
SQL Server dynamic PIVOT query?

Is it possible to execute a query on the following table:

Game    Player  Goals
-----   ------  ------
Game1   John    1
Game1   Paul    0
Game1   Mark    2
Game1   Luke    1
Game2   John    3
Game2   Paul    1   
Game2   Luke    1
Game3   John    0
Game3   Mark    2

which gives a result like this:

Game    John    Paul    Mark    Luke
-----   ----    ----    ----    ----
Game1   1       0       2       1
Game2   3       1       -       1
Game3   0       -       2       -

It turns each distinct player into a column and groups the games gives goals per game per player.

  • 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-14T09:20:08+00:00Added an answer on June 14, 2026 at 9:20 am

    You can use the PIVOT function. If you have a known number of columns, then you can hard-code the values:

    select *
    from
    (
      select game, player, goals
      from yourtable
    ) src
    pivot
    (
      sum(goals)
      for player in ([John], [Paul], [Mark], [Luke])
    ) piv
    order by game
    

    See SQL Fiddle with Demo

    If you have an unknown number of columns, then you can use dynamic sql:

    DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    select @cols = STUFF((SELECT distinct ',' + QUOTENAME(player) 
                        from yourtable
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    set @query = 'SELECT game, ' + @cols + ' from 
                 (
                    select game, player, goals
                    from yourtable
                ) x
                pivot 
                (
                    sum(goals)
                    for player in (' + @cols + ')
                ) p '
    
    execute(@query)
    

    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

Possible Duplicate: SQL Server dynamic PIVOT query? I have temporary table with following structure:
Possible Duplicate: dynamic sql pivot in sql server I have a table called Col_values
Possible Duplicate: SQL Server dynamic PIVOT query? I have a dataset that has the
Possible Duplicate: dynamic sql pivot in sql server How to alter this stored procedure
Possible Duplicate: SQL Server Database query help Hi, I have a problem that I
Possible Duplicate: Saving changes after table edit in SQL Server Management Studio I need
Possible Duplicate: SQL Server: Only last entry in GROUP BY I have a table
Possible Duplicate: delete duplicate records in SQL Server I have a table in which
Possible Duplicate: Row Offset in SQL Server I have a query which has a
Possible Duplicate: SQL Comments on Create Table on SQL Server 2008 I just want

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.