成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

SQLServer中怎么將行數(shù)據(jù)轉(zhuǎn)換為列數(shù)據(jù)

今天就跟大家聊聊有關(guān)SQL Server中怎么將行數(shù)據(jù)轉(zhuǎn)換為列數(shù)據(jù),可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計制作、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的寶山網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

準備工作

創(chuàng)建表

use [test1]gocreate table [dbo].[student](  [id] [int] identity(1,1) not null,  [name] [nvarchar](50) null,  [project] [nvarchar](50) null,  [score] [int] null, constraint [pk_student] primary key clustered (  [id] asc)with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary]) on [primary]go

插入數(shù)據(jù)

insert into test1.dbo.student(name,project,score)values('張三','android','60'),   ('張三','ios','70'),   ('張三','html5','55'),   ('張三','.net','100'),   ('李四','android','60'),   ('李四','ios','75'),   ('李四','html5','90'),   ('李四','.net','100');

使用Case When和聚合函數(shù)進行行專列

語法

select column_name,<aggregation function>(<case when expression>) from database.schema.tablegroup by column_name

語法解析

column_name

數(shù)據(jù)列列名

aggregation function

聚合函數(shù),常見的有:sum,max,min,avg,count等。

case when expression

case when表達式

示例

select name,max(case project when 'android' then score end) as '安卓',max(case project when 'ios' then score end) as '蘋果',max(case project when 'html5' then score end) as 'html5',max(case project when '.net' then score end) as '.net'from [test1].[dbo].[student]group by name

示例結(jié)果

轉(zhuǎn)換前

轉(zhuǎn)換后

使用PIVOT進行行專列

PIVOT通過將表達式中一列中的唯一值轉(zhuǎn)換為輸出中的多個列來旋轉(zhuǎn)表值表達式。并PIVOT在最終輸出中需要的任何剩余列值上運行聚合,PIVOT提供比一系列復雜的SELECT...CASE語句指定的語法更為簡單和可讀的語法,PIVOT執(zhí)行聚合并將可能的多行合并到輸出中的單個行中。

語法

select <non-pivoted column>,   [first pivoted column] as <column name>,   [second pivoted column] as <column name>,   ...   [last pivoted column] as <column name> from   (<select query that produces the data>)    as <alias for the source query> pivot (   <aggregation function>(<column being aggregated>) for  [<column that contains the values that will become column headers>]    in ( [first pivoted column], [second pivoted column],   ... [last pivoted column]) ) as <alias for the pivot table> <optional order by clause>;

語法解析

<non-pivoted column>

非聚合列。

[first pivoted column]

第一列列名。

[second pivoted column]

第二列列名。

[last pivoted column]

最后一列列名。

<select query that produces the data>

數(shù)據(jù)子表。

<alias for the source query>

表別名。

<aggregation function>

聚合函數(shù)。

<column being aggregated>

聚合函數(shù)列,用于輸出值列,最終輸出中返回的列(稱為分組列)將對其進行分組。

[<column that contains the values that will become column headers>]

轉(zhuǎn)換列,此列返回的唯一值將成為最終結(jié)果集中的字段。

[first pivoted column], [second pivoted column], ... [last pivoted column]

數(shù)據(jù)行中每一行行要轉(zhuǎn)換的列名。

<optional order by clause>

排序規(guī)則。

示例

select b.Name,b.[android],b.[ios],b.[html5],b.[.net] from (select Name,Project,Score from [test1].[dbo].[student])as apivot(  max(Score)  for Project in ([android],[ios],[html5],[.net])) as border by b.name desc

1、如果輸出列名不能在表轉(zhuǎn)換列中,則不會執(zhí)行任何計算。

2、輸出的所有列的列名的數(shù)據(jù)類型必須一致。

看完上述內(nèi)容,你們對SQL Server中怎么將行數(shù)據(jù)轉(zhuǎn)換為列數(shù)據(jù)有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

網(wǎng)站欄目:SQLServer中怎么將行數(shù)據(jù)轉(zhuǎn)換為列數(shù)據(jù)
網(wǎng)站路徑:http://jinyejixie.com/article18/ppjjdp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、品牌網(wǎng)站建設(shè)面包屑導航、微信公眾號、網(wǎng)站改版、定制開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)
合川市| 库车县| 威信县| 商南县| 五大连池市| 杭锦旗| 肃北| 寻甸| 北辰区| 呼和浩特市| 株洲市| 大丰市| 台州市| 东光县| 离岛区| 蒙山县| 建阳市| 疏勒县| 高青县| 仁布县| 邢台县| 肥城市| 天水市| 鹤峰县| 康定县| 和静县| 乌拉特后旗| 合作市| 左权县| 远安县| 高尔夫| 正蓝旗| 阜南县| 福鼎市| 池州市| 华安县| 西昌市| 宜州市| 西林县| 长葛市| 瑞昌市|