国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > SQL Server > 正文

SQL Server基礎(chǔ)之行數(shù)據(jù)轉(zhuǎn)換為列數(shù)據(jù)

2020-07-25 12:36:09
字體:
供稿:網(wǎng)友

準(zhǔn)備工作

創(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ù)進(jìn)行行專列

語法

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表達(dá)式

示例

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進(jìn)行行專列

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

語法

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ù)列,用于輸出值列,最終輸出中返回的列(稱為分組列)將對(duì)其進(jìn)行分組。

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

示例結(jié)果

轉(zhuǎn)換前

轉(zhuǎn)換后

注意事項(xiàng)

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

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

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)武林網(wǎng)的支持。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 万州区| 金塔县| 车致| 额济纳旗| 吉林市| 博野县| 大方县| 民县| 磐石市| 大安市| 罗江县| 忻州市| 灵石县| 正镶白旗| 宜丰县| 舟山市| 九台市| 西丰县| 平阳县| 宝丰县| 晋江市| 铜鼓县| 达州市| 布尔津县| 吉安市| 上饶市| 自贡市| 壤塘县| 万宁市| 迭部县| 阿克陶县| 阿克苏市| 宿迁市| 太仆寺旗| 兰坪| 绍兴县| 上栗县| 清徐县| 灯塔市| 江山市| 额尔古纳市|