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

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

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

問題描述

限時送ChatGPT賬號..

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

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模板網!

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

相關文檔推薦

What SQL Server Datatype Should I Use To Store A Byte[](我應該使用什么 SQL Server 數據類型來存儲字節 [])
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?)
主站蜘蛛池模板: 一区二区三区在线电影 | xnxx 日本免费 | 天天干天天玩天天操 | 国产精品一区二区在线 | 欧美日韩最新 | 日韩av福利在线观看 | 夜夜艹 | av看看| 91在线一区二区三区 | 香蕉视频在线播放 | 欧美日韩一区二区在线播放 | 玖玖视频网| www视频在线观看 | 亚洲国产日本 | 欧美精品久久久久久 | 国内精品久久精品 | 天天躁人人躁人人躁狂躁 | 日日天天 | 精品久草 | 亚洲高清一区二区三区 | 久久精品毛片 | 亚洲色图插插插 | 99自拍视频 | 免费久久久久久 | 蜜桃在线视频 | 精品国产乱码久久久久久闺蜜 | 黄色网址大全在线观看 | 鸡毛片| 性色网站| 中文字幕日韩在线观看 | 国产精品成人国产乱一区 | 亚洲精品视频免费观看 | 精品在线99| 中文字幕日韩一区 | 最新国产在线 | 亚洲一二三区在线观看 | 偷拍亚洲色图 | 国产精品久久久久无码av | 日本在线免费看最新的电影 | 男女免费网站 | 日韩在线免费播放 |