久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何從單行創建多行 - 規范化表

How to create multiple rows from a single row - normalize a table(如何從單行創建多行 - 規范化表)
本文介紹了如何從單行創建多行 - 規范化表的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我對 SQL 很陌生,正在嘗試解決這個問題:

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

我有一個名為 BUDGET 的表,其中包含一年中每個月的 12 列,顯示該月的預算余額.所以表格看起來像這樣:

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

我想要做的是標準化這個表并將每一行分成 12 行,每行有一個日期字段,格式如下.我還想有一個 [余額] 列來顯示該月的值.因此,規范化的表將如下所示:

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          .......      ......

我嘗試在同一張桌子上使用 CROSS JOIN 但失敗了.我也嘗試使用 while 循環,但也失敗了.任何形式的幫助表示贊賞.謝謝!

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!

我使用的是 SQL Server 2008

I am using SQL Server 2008

推薦答案

為了好玩,這里有一個 CROSS APPLY 解決方案:

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);

它與@Aaron Bertrand 的 UNPIVOT 沒有什么不同,沒有使用 UNPIVOT.

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

如果您必須將日期作為字符串,則將字符串放入 CROSS APPLY 中,如 ('01', Month1) 并將 SELECT 更改為 Convert(char(4),B.Year) + M.Mo.

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.

這篇關于如何從單行創建多行 - 規范化表的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創建視圖?)
Create calculated value based on calculated value inside previous row(根據前一行內的計算值創建計算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 久久久.com| 在线观看视频中文字幕 | 久久国产精品网站 | 欧美精品在线一区 | 91久久久精品国产一区二区蜜臀 | 国产成人精品网站 | 国产精久久久久久久妇剪断 | 午夜在线影院 | 99精品视频一区二区三区 | 91精品国产91久久久久久不卞 | 欧美三区 | 一级a爱片久久毛片 | 在线看亚洲 | 国产精品一区视频 | 我想看国产一级毛片 | 91视频进入 | 久久精品一级 | 午夜影院网站 | 成人中文字幕在线 | 国产成人精品免高潮在线观看 | 国产丝袜人妖cd露出 | 日韩在线精品视频 | 久久久久久中文字幕 | 日本黄色一级片视频 | 亚洲成人精品国产 | 亚洲一区国产精品 | 久久国产精品免费一区二区三区 | 九九免费视频 | 国产99热 | 狠狠干天天干 | 精品一级毛片 | 91正在播放 | 天堂久久天堂综合色 | 欧美精品一区二区三区蜜桃视频 | 欧美一区二区在线 | 欧美综合一区二区三区 | 91视频.com | www.一区二区三区 | 欧美日韩国产在线观看 | 精精国产xxxx视频在线野外 | 337p日本欧洲亚洲大胆 |