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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:25:27+00:00 2026-05-13T18:25:27+00:00

DB: SQL Server 2005 We have a table that has data in this manner:

  • 0

DB: SQL Server 2005

We have a table that has data in this manner:

Project              Year        Jan                   Feb                   Mar                   Apr                   May                   Jun                   Jul                   Aug                   Sep                   Oct                   Nov                   Dec
-------------------- ----------- --------------------- --------------------- --------------------- --------------------- --------------------- --------------------- --------------------- --------------------- --------------------- --------------------- --------------------- ---------------------
11-11079             2008        0.00                  0.00                  0.00                  0.00                  0.00                  0.00                  0.00                  0.00                  0.00                  0.00                  0.00                  75244.90
11-11079             2009        466.00                0.00                  0.00                  0.00                  0.00                  0.00                  0.00                  0.00                  0.00                  0.00                  0.00                  0.00
11-11079             2010        855.00                0.00                  0.00                  0.00                  0.00                  0.00                  0.00                  0.00                  0.00                  0.00                  0.00                  0.00  
01-11052             2009        56131.00              0.00                  36962.00              -61596.00             2428.00               84.00                 0.00                  0.00                  0.00                  0.00                  0.00                  0.00

Someone would like the data to be displayed as one row for the entire project. The columns would be dynamic dependant on how many years it goes into the future. An example would be:

Project        Jan-2009     Feb-2009     Mar-2009     Apr-2009... Dec-2009     Jan-2010
-------------- ------------ ------------ ------------ ----------- ------------ ---------
11-11079       466.00       0.00         0.00         0.00        0.00         855.00    
01-11052       56131.00     0.00         36962.00     -61596.00   2428.00      0.00

I read of many examples where the date is populated in one column for each entry but I haven’t found any cases where the months are the column name and the year is in the row.

Dynamic SQL with a pivot table?
Or some pretty wide scale manipulation using SQL, temp tables, joins and unions?
Any thoughts on using the SSIS pivot table feature?

  • 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-05-13T18:25:27+00:00Added an answer on May 13, 2026 at 6:25 pm

    Your data is already pivoted, but needs to be pivoted at a different level. I think the best way to handle this is to unpivot it first, then handle the correct pivot level second.

    Step 1: Unpivot

    You can use the SQL 2005 UNPIVOT command, or use a CROSS JOIN technique. Here are examples of both. Note I left out months in the middle to keep things simple. Just add them in.

    -- CROSS JOIN method (also works in SQL 2000)
    SELECT
       P.Project,
       Mo =
          DateAdd(mm,
             X.MonthNum,
             DateAdd(yy, P.[Year] - 1900, '19000101')
          ),
       Amount = 
          CASE X.MonthNum
             WHEN 0 THEN Jan
             WHEN 1 THEN Feb
             WHEN 11 THEN Dec
          END
    FROM
       ProjectData P
       CROSS JOIN (
          SELECT 0 UNION ALL SELECT 1 UNION ALL SELECT 11
       ) X (MonthNum)
    

    Each row is repeated 12 times, then a CASE statement pulls out only one month for each row, leaving the data nicely unpivoted.

    -- UNPIVOT method
    SELECT
        P.Project,
        Mo =
           DateAdd(mm,
              Convert(int, P.MonthNum),
              DateAdd(yy, P.[Year] - 1900, '19000101')
           ),
        P.Amount
    FROM
       (
          SELECT Project, [Year], [0] = Jan, [1] = Feb, [11] = Dec
          FROM ProjectData
       ) X UNPIVOT (Amount FOR MonthNum IN ([0], [1], [11])) P
    
    DROP TABLE ProjectData
    

    Neither method is a clear performance winner all the time. Sometimes one works better than the other (depending on the data being pivoted). The UNPIVOT method uses a Filter in the execution plan that the CROSS JOIN does not.

    Step 2: Pivot Again

    Now, how to use the unpivoted data. You didn’t say how your someone will be consuming this, but since you’ll need to put the data in an output file of some kind, I propose using SSRS (Sql Server Reporting Services), which comes with SQL Server 2005 for no extra charge.

    Just use the Matrix report object to pivot one of the queries above. This object happily determines the data values to make into column labels at report run-time and sounds like exactly what you need. If you add a column that formats the date exactly how you like, then you can order by the Mo column, but use the new expression as the column label.

    SSRS also has a wide variety of formats and scheduling options available. For example, you can have it email an Excel file or save a web page to a file share.

    Please let me know if I’ve left anything out.

    For anyone who would like to see the code above in action, here’s some creation script for you:

    USE tempdb
    
    CREATE TABLE ProjectData (
        Project varchar(10),
        [Year] int,
        Jan decimal(15, 2),
        Feb decimal(15, 2),
        Dec decimal(15, 2)
    )
    
    SET NOCOUNT ON
    
    INSERT ProjectData VALUES ('11-11079', 2008, 0.0, 0.0, 75244.90)
    INSERT ProjectData VALUES ('11-11079', 2009, 466.0, 0.0, 0.0)
    INSERT ProjectData VALUES ('11-11079', 2010, 855.0, 0.0, 0.0)
    INSERT ProjectData VALUES ('01-11052', 2009, 56131.0, 0.0, 0.0)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 437k
  • Answers 437k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You may not realize it, but you are doing method… May 15, 2026 at 4:19 pm
  • Editorial Team
    Editorial Team added an answer one-to-one is not supported for implicit polymorphism. Alternatives: Create a… May 15, 2026 at 4:19 pm
  • Editorial Team
    Editorial Team added an answer Since my comment seemed to provide the correct answer, I… May 15, 2026 at 4:19 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.