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

如何將列轉置為 sql server 中的行

How to transpose columns to rows in sql server(如何將列轉置為 sql server 中的行)
本文介紹了如何將列轉置為 sql server 中的行的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我正在嘗試獲取一個原始數據集,為新數據添加列并將其轉換為更傳統(tǒng)的表結構.這個想法是讓腳本提取列名稱(日期)并將其放入一個新列中,然后將每個日期數據值堆疊在一起.

I'm trying to take a raw data set that adds columns for new data and convert it to a more traditional table structure. The idea is to have the script pull the column name (the date) and put that into a new column and then stack each dates data values on top of each other.

示例

Store     1/1/2013     2/1/2013
XYZ INC   $1000        $2000

Store     Date         Value
XYZ INC   1/1/2013     $1000
XYZ INC   2/1/2013     $2000

謝謝

推薦答案

有幾種不同的方法可以獲得您想要的結果.

There are a few different ways that you can get the result that you want.

您可以將 SELECTUNION ALL 一起使用:

You can use a SELECT with UNION ALL:

select store, '1/1/2013' date, [1/1/2013] value
from yourtable
union all
select store, '2/1/2013' date, [2/1/2013] value
from yourtable;

參見SQL Fiddle with Demo.

您可以使用 UNPIVOT 函數:

You can use the UNPIVOT function:

select store, date, value
from yourtable
unpivot
(
  value
  for date in ([1/1/2013], [2/1/2013])
) un;

參見 SQL Fiddle with Demo.

最后,根據您的 SQL Server 版本,您可以使用 CROSS APPLY:

Finally, depending on your version of SQL Server you can use CROSS APPLY:

select store, date, value
from yourtable
cross apply
(
  values
    ('1/1/2013', [1/1/2013]),
    ('2/1/2013', [2/1/2013])
) c (date, value)

請參閱SQL Fiddle with Demo.所有版本都會給出以下結果:

See SQL Fiddle with Demo. All versions will give a result of:

|   STORE |     DATE | VALUE |
|---------|----------|-------|
| XYZ INC | 1/1/2013 |  1000 |
| XYZ INC | 2/1/2013 |  2000 |

這篇關于如何將列轉置為 sql server 中的行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

What SQL Server Datatype Should I Use To Store A Byte[](我應該使用什么 SQL Server 數據類型來存儲字節(jié) [])
Interpreting type codes in sys.objects in SQL Server(解釋 SQL Server 中 sys.objects 中的類型代碼)
Typeorm .loadRelationCountAndMap returns zeros(Typeorm .loadRelationCountAndMap 返回零)
MS SQL: Should ISDATE() Return quot;1quot; when Cannot Cast as Date?(MS SQL:ISDATE() 是否應該返回“1?什么時候不能投射為日期?)
Converting the name of a day to its integer representation(將一天的名稱轉換為其整數表示)
How to convert nvarchar m/d/yy to mm/dd/yyyy in SQL Server?(如何在 SQL Server 中將 nvarchar m/d/yy 轉換為 mm/dd/yyyy?)
主站蜘蛛池模板: 99免费看| 日本激情视频在线播放 | 国产日韩视频在线 | 狠狠av| 黄色成人在线观看 | 久久日本 | 91超碰在线观看 | 亚洲一区二区在线电影 | 国产精品福利视频 | 国产一区二区影院 | 亚洲成人综合社区 | 视频一区二区三区在线观看 | 国产最新视频在线 | 久色网 | 亚洲午夜精品一区二区三区他趣 | 热久色 | 久久国产欧美日韩精品 | 欧美精品一区二区在线观看 | h片在线观看免费 | 欧洲精品在线观看 | 亚洲一区二区免费视频 | 精品久久久久久久久久久久久久 | 国产精品一区在线播放 | 黄色av网站在线免费观看 | 欧美福利视频 | 成人网在线观看 | 日韩免费激情视频 | 欧美一区 | 成人精品久久 | 黄片毛片免费看 | 国产高清在线 | 另类亚洲视频 | 国产精品免费一区二区三区 | 精品久久久久久亚洲精品 | 激情毛片 | 国产成人一区二区三区久久久 | 亚洲97 | 婷婷在线视频 | 毛色毛片免费看 | 天天操天天舔 | 在线中文字幕第一页 |