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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:23:18+00:00 2026-06-09T03:23:18+00:00

I am pretty new to SQL and trying to figure this out: I have

  • 0

I am pretty new to SQL and trying to figure this out:

I have a table called BUDGET that has 12 columns for each month of the year, displaying the budget balance of that month. So the table looks like this:

[Department]  [Year]  [Month1] [Month2] .... [Month12]  
ABCD           2010   $5000     $5500   .....  $4000
ABCD           2011   $6000     $6500   .....  $3000

What I am trying to do is to normalize this table and break each row into 12 rows, each row with a date field in the following format. I also want to have a [Balance] column that displays the value of that month. So, the normalized table will look like this:

[Department]  [Date]     [Balance] 
ABCD          20100101     $5000   
ABCD          20100201     $5500 
ABCD          20100301     .....
ABCD          .......      ......

I tried using CROSS JOIN on the same table but failed. I also tried using a while loop but that failed as well. Any kind of help is appreciated. Thanks!

EDIT: I am using SQL Server 2008

  • 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-09T03:23:19+00:00Added an answer on June 9, 2026 at 3:23 am

    Just for fun here’s a CROSS APPLY solution:

    SELECT
       B.Department,
       DateAdd(month, (B.Year - 1900) * 12 + M.Mo - 1, 0) [Date],
       M.Balance
    FROM
       dbo.Budget B
       CROSS APPLY (
          VALUES
          (1, Month1), (2, Month2), (3, Month3), (4, Month4), (5, Month5), (6, Month6),
          (7, Month7), (8, Month8), (9, Month9), (10, Month10), (11, Month11), (12, Month12)
       ) M (Mo, Balance);
    

    It’s really no different than @Aaron Bertrand’s UNPIVOT, without using UNPIVOT.

    If you must have the date as a string, then put strings in the CROSS APPLY like ('01', Month1) and change the SELECT to Convert(char(4), B.Year) + M.Mo.

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

Sidebar

Related Questions

I'm pretty new to SQL, and I'm trying to do something like this: I
I'm pretty new to SQL. I have a database with records based on road/milepoints.
I am pretty much new to using SQL Server 2000. I have been using
I am pretty new to C# and SQL Server database. I have created a
I am pretty new to PHP and MySQL and I just can't figure this
I'm pretty new to SQL, and am trying to do something not the dumb
So I'm pretty new to Visual Basic and inherited this VB6 code that i
I'm new to SQL and database design and trying to work out if the
I think this should be pretty simple, but I'm a SQL newb. I have
I'm pretty new to this so please bear with me. I'm trying to make

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.