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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:31:25+00:00 2026-06-04T15:31:25+00:00

Possible Duplicate: SQL Server dynamic PIVOT query? I have temporary table with following structure:

  • 0

Possible Duplicate:
SQL Server dynamic PIVOT query?

I have temporary table with following structure:

MONTH  ID           CNT
-----  -----------  ---
4      TOTAL_COUNT  214
5      TOTAL_COUNT  23
6      TOTAL_COUNT  23
4      FUNC_COUNT   47
5      FUNC_COUNT   5
6      FUNC_COUNT   5
4      INDIL_COUNT  167
5      INDIL_COUNT  18
6      INDIL_COUNT  18

How i can get the Pivot over month in this table like:

ID           APRIL  MAY  JUNE 
-----------  -----  ---  ----
TOTAL_COUNT  214    23   23
FUNC_COUNT   47     5    5
INDIL_COUNT  167    18   18

Please consider this table format. I am little messy in posting this format.

  • 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-04T15:31:27+00:00Added an answer on June 4, 2026 at 3:31 pm

    While you can use a Static Pivot – one that you hard-code the months. In the comments, you stated that the number of months maybe be unknown, if that is the case then you will want to use a Dynamic Pivot to generate the list of months. Using a Dynamic Pivot gives you the flexibility of not knowing the columns you need until you run it.

    create table t
    ( 
        [month] int, 
        [id] nvarchar(20), 
        [cnt] int 
    )
    
    insert t values (4,'TOTAL_COUNT',214)
    insert t values (5,'TOTAL_COUNT',23)
    insert t values (6,'TOTAL_COUNT',23)
    insert t values (4,'FUNC_COUNT',47)
    insert t values (5,'FUNC_COUNT',5)
    insert t values (6,'FUNC_COUNT',5)
    insert t values (4,'INDIL_COUNT',167)
    insert t values (5,'INDIL_COUNT',18)
    insert t values (6,'INDIL_COUNT',18)
    
    DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX);
    
    select @cols = STUFF((SELECT distinct ',' + QUOTENAME(month) 
                FROM t 
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    set @query = 'SELECT id, ' + @cols + ' from 
                (
                    select month, id, cnt
                    from t
               ) x
                pivot 
                (
                     sum(cnt)
                    for month in (' + @cols + ')
                ) p '
    
    
    execute(@query)
    
    drop table t
    

    The results would be:

    enter image description here

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

Sidebar

Related Questions

Possible Duplicate: dynamic sql pivot in sql server I have a table called Col_values
Possible Duplicate: SQL Server Database query help Hi, I have a problem that I
Possible Duplicate: Writing an SQL query to SELECT item from the following table I
Possible Duplicate: dynamic sql pivot in sql server How to alter this stored procedure
Possible Duplicate: SQL Server: Only last entry in GROUP BY I have a table
Possible Duplicate: Function to Calculate Median in Sql Server I have a table like
Possible Duplicate: Select n random rows from SQL Server table I have database and
Possible Duplicate: Saving changes after table edit in SQL Server Management Studio I need
Possible Duplicate: SQL Comments on Create Table on SQL Server 2008 I just want
Possible Duplicate: Does SQL Server 2005 have an equivalent to MySql’s ENUM data type?

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.