How to create multiple rows from a single row - no

2019-06-03 06:38发布

问题:

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:

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.



回答2:

SELECT 
  Department, 
  [Date] = DATEADD(MONTH, CONVERT(INT, SUBSTRING([Month],6,2))-1, 
     DATEADD(YEAR, [Year]-1900, 0)), 
  Balance
FROM
  dbo.BUDGET AS b
  UNPIVOT 
  (
    Balance FOR [Month] IN 
    (
      Month1, Month2,  Month3,  Month4,
      Month5, Month6,  Month7,  Month8,
      Month9, Month10, Month11, Month12
    )
  ) AS y
ORDER BY Department, [Date];


回答3:

This is how I do it. No need to get all fancy about it.

select department = b.department ,
       year       = b.year       ,
       month      = m.month      ,
       balance    = case m.month
                    when  1 then b.Month1
                    when  2 then b.Month2
                    when  3 then b.Month3
                    when  4 then b.Month4
                    when  5 then b.Month5
                    when  6 then b.Month6
                    when  7 then b.Month7
                    when  8 then b.Month8
                    when  9 then b.Month9
                    when 10 then b.Month10
                    when 11 then b.Month11
                    when 12 then b.Month12
                    else         null
                    end
from dbo.budget b
join (           select month =  1
       union all select month =  2
       union all select month =  3
       union all select month =  4
       union all select month =  5
       union all select month =  6
       union all select month =  7
       union all select month =  8
       union all select month =  9
       union all select month = 10
       union all select month = 11
       union all select month = 12
     ) m on 1 = 1  -- a dummy join: we want the cartesian product here so as to expand every row in budget into twelve, one per month of the year.