SQL Server數(shù)據(jù)庫基礎(chǔ)之行數(shù)據(jù)轉(zhuǎn)換為列數(shù)據(jù)
文章主要給大家介紹了關(guān)于SQL Server基礎(chǔ)之行數(shù)據(jù)轉(zhuǎn)換為列數(shù)據(jù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用SQL Server具有一定的參考學(xué)習(xí)價值,需要的朋友們...
文章主要給大家介紹了關(guān)于SQL Server基礎(chǔ)之行數(shù)據(jù)轉(zhuǎn)換為列數(shù)據(jù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用SQL Server具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
準(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)行行專列
語法
select column_name,
<aggregation function>(<case when expression>)
from database.schema.table
group 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)換為輸出中的多個列來旋轉(zhuǎn)表值表達(dá)式。并PIVOT在最終輸出中需要的任何剩余列值上運(yùn)行聚合,PIVOT提供比一系列復(fù)雜的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ù)列,用于輸出值列,最終輸出中返回的列(稱為分組列)將對其進(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)換列中,則不會執(zhí)行任何計算。
2、輸出的所有列的列名的數(shù)據(jù)類型必須一致。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值。
- SQL server數(shù)據(jù)庫創(chuàng)建代碼 filegroup文件組修改的示例
- SQLServer數(shù)據(jù)庫處于恢復(fù)掛起狀態(tài)的解決辦法
- SQL Server數(shù)據(jù)庫之datepart和datediff應(yīng)用查找當(dāng)天上
- SQL Server數(shù)據(jù)庫中的數(shù)據(jù)類型隱式轉(zhuǎn)換問題
- Thinkphp5框架實(shí)現(xiàn)獲取數(shù)據(jù)庫數(shù)據(jù)到視圖的方法
- Linux下使用ps命令來查看oracle數(shù)據(jù)庫相關(guān)進(jìn)程的操作
- 如何使用Access數(shù)據(jù)庫創(chuàng)建一個簡單MIS管理系統(tǒng)
- Access數(shù)據(jù)庫日常維護(hù)和Access數(shù)據(jù)庫優(yōu)化方法
- MariaDB數(shù)據(jù)庫的外鍵約束實(shí)例代碼介紹詳解
- Windows10系統(tǒng)下MariaDB數(shù)據(jù)庫安裝教程圖解
SQL server數(shù)據(jù)庫創(chuàng)建代碼 filegroup文件組修改的示例代碼
文章主要介紹了SQL server數(shù)據(jù)庫創(chuàng)建代碼 filegroup文件組修改的實(shí)現(xiàn)方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下數(shù)據(jù)庫的操作:1. 對數(shù)據(jù)文件...
SQLServer數(shù)據(jù)庫處于恢復(fù)掛起狀態(tài)的解決辦法
文章主要介紹了SQLServer數(shù)據(jù)庫處于恢復(fù)掛起狀態(tài)的解決辦法 ,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下.一、總結(jié) 如果數(shù)據(jù)庫處...
SQL Server數(shù)據(jù)庫之datepart和datediff應(yīng)用查找當(dāng)天上午和下午的數(shù)據(jù)
文章主要介紹了sqlserver之datepart和datediff應(yīng)用查找當(dāng)天上午和下午的數(shù)據(jù),非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下DATEPART() 函數(shù)用于返回日期/時間的單獨(dú)...
SQL Server數(shù)據(jù)庫中的數(shù)據(jù)類型隱式轉(zhuǎn)換問題
文章主要介紹了SQL Server 中的數(shù)據(jù)類型隱式轉(zhuǎn)換問題,本文給大家介紹的非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下寫這篇文章的時候,還真不知道如何取名,也不知道這...
Thinkphp5框架實(shí)現(xiàn)獲取數(shù)據(jù)庫數(shù)據(jù)到視圖的方法
文章主要介紹了Thinkphp5框架實(shí)現(xiàn)獲取數(shù)據(jù)庫數(shù)據(jù)到視圖的方法,涉及thinkPHP5數(shù)據(jù)庫配置、讀取、模型操作及視圖調(diào)用相關(guān)操作技巧,需要的朋友可以參考下。這是學(xué)習(xí)thinkhp5的...
Linux下使用ps命令來查看oracle數(shù)據(jù)庫相關(guān)進(jìn)程的操作步驟
ps命令的操作是很多的小伙伴在管理進(jìn)程的操作的時候遇到的問題,對于Linux系統(tǒng)中今天小編就來跟大家分享一下詳解Oracle相關(guān)進(jìn)程在電腦中使用ps命令查看的操作步驟。...
如何使用Access數(shù)據(jù)庫創(chuàng)建一個簡單MIS管理系統(tǒng)
MIS管理系統(tǒng)也是一種很實(shí)用的管理系統(tǒng),可以將很多東西都放的井井有條,便于大家查找,下文中就以大家家中都有的CD、VCD為例,為大家介紹如何建立一個MIS管理系統(tǒng),使這些東西有條理。...
Access數(shù)據(jù)庫日常維護(hù)和Access數(shù)據(jù)庫優(yōu)化方法
文章主要介紹了Access數(shù)據(jù)庫日常維護(hù)方法(優(yōu)化),適用范圍:使用Access作為數(shù)據(jù)庫建設(shè)的網(wǎng)站。需要的朋友可以參考下...
MariaDB數(shù)據(jù)庫的外鍵約束實(shí)例代碼介紹詳解
文章主要給大家介紹了關(guān)于MariaDB數(shù)據(jù)庫的外鍵約束的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧...
Windows10系統(tǒng)下MariaDB數(shù)據(jù)庫安裝教程圖解
文章給大家介紹Windows10系統(tǒng)下安裝MariaDB 的教程圖解,感興趣的朋友一起看看吧,MariaDB由MySQL的創(chuàng)始人麥克爾·維德紐斯主導(dǎo)開發(fā),...