濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > SQL Server基礎(chǔ)之行數(shù)據(jù)轉(zhuǎn)換為列數(shù)據(jù)

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

熱門標(biāo)簽:武漢長(zhǎng)沙外呼系統(tǒng)方法和技巧 優(yōu)質(zhì)地圖標(biāo)注 京華物流公司地圖標(biāo)注 奧威地圖標(biāo)注多個(gè)地方 千呼電銷機(jī)器人價(jià)格 智能語(yǔ)音外呼系統(tǒng)選哪家 外呼系統(tǒng)電銷專用 怎樣在地圖上標(biāo)注路線圖標(biāo) 百度地圖標(biāo)注不同路線

準(zhǔn)備工作

創(chuàng)建表

use [test1]
go

create 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)行行專列

語(yǔ)法

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

語(yǔ)法解析

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

語(yǔ)法

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

語(yǔ)法解析

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 a
pivot
(
  max(Score)
  for Project in ([android],[ios],[html5],[.net])
) 
as b
order by b.name desc

示例結(jié)果

轉(zhuǎn)換前

轉(zhuǎn)換后

注意事項(xiàng)

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

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

總結(jié)

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

您可能感興趣的文章:
  • SQL Server將一列的多行內(nèi)容拼接成一行的實(shí)現(xiàn)方法
  • SQLServer行轉(zhuǎn)列實(shí)現(xiàn)思路記錄
  • Sql Server 2000 行轉(zhuǎn)列的實(shí)現(xiàn)(橫排)
  • sqlserver2005 行列轉(zhuǎn)換實(shí)現(xiàn)方法
  • sqlserver下將數(shù)據(jù)庫(kù)記錄的列記錄轉(zhuǎn)換成行記錄的方法
  • sqlserver 行列互轉(zhuǎn)實(shí)現(xiàn)小結(jié)
  • SQLServer行列互轉(zhuǎn)實(shí)現(xiàn)思路(聚合函數(shù))
  • SQL Server行轉(zhuǎn)列的方法解析

標(biāo)簽:威海 防疫戰(zhàn)設(shè) 宿州 來(lái)賓 天水 銅仁 七臺(tái)河 益陽(yáng)

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《SQL Server基礎(chǔ)之行數(shù)據(jù)轉(zhuǎn)換為列數(shù)據(jù)》,本文關(guān)鍵詞  SQL,Server,基礎(chǔ),之行,數(shù)據(jù),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《SQL Server基礎(chǔ)之行數(shù)據(jù)轉(zhuǎn)換為列數(shù)據(jù)》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于SQL Server基礎(chǔ)之行數(shù)據(jù)轉(zhuǎn)換為列數(shù)據(jù)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    潮安县| 永济市| 莱阳市| 兴安县| 巴彦淖尔市| 阿图什市| 昌图县| 阳山县| 黄石市| 博白县| 文水县| 永嘉县| 芷江| 江源县| 库尔勒市| 敦煌市| 平陆县| 遂宁市| 阿拉善右旗| 浙江省| 万山特区| 兴山县| 射洪县| 读书| 绥芬河市| 沿河| 南皮县| 崇仁县| 堆龙德庆县| 南安市| 灵宝市| 日土县| 资兴市| 浠水县| 武功县| 息烽县| 安阳县| 大名县| 同德县| 山丹县| 宁武县|