SQL Server使用PIVOT與unPIVOT實(shí)現(xiàn)行列轉(zhuǎn)換
一、sql行轉(zhuǎn)列:PIVOT
1、基本語(yǔ)法:
create table #table1 ( id int ,code varchar(10) , name varchar(20) ); go insert into #table1 ( id,code, name ) values ( 1, 'm1','a' ), ( 2, 'm2',null ), ( 3, 'm3', 'c' ), ( 4, 'm2','d' ), ( 5, 'm1','c' ); go select * from #table1; --方法一(推薦) select PVT.code, PVT.a, PVT.b, PVT.c from #table1 pivot(count(id) for name in(a, b, c)) as PVT; --方法二 with P as (select * from #table1) select PVT.code, PVT.a, PVT.b, PVT.c from P pivot(count(id) for name in(a, b, c)) as PVT; drop table #table1;
結(jié)果:
2、實(shí)例:
3、傳統(tǒng)方式:(先匯總拼接出所需列的字符串,再動(dòng)態(tài)執(zhí)行轉(zhuǎn)列)
先查詢出要轉(zhuǎn)為列的行數(shù)據(jù),再拼接字符串。
create table #table1 ( id int ,code varchar(10) , name varchar(20) ); go insert into #table1 ( id,code, name ) values ( 1, 'm1','a' ), ( 2, 'm2',null ), ( 3, 'm3', 'c' ), ( 4, 'm2','d' ), ( 5, 'm1','c' ); go select * from #table1; declare @strCN nvarchar(100); select @strCN = isnull(@strCN + ',', '') + quotename(name) from #table1 group by name ; print @strCN --‘[a],[c],[d]' declare @SqlStr nvarchar(1000); set @SqlStr = N' select * from #table1 pivot ( count(ID) for name in (' + @strCN + N') ) as PVT'; exec ( @SqlStr ); drop table #table1;
結(jié)果:
二、sql列轉(zhuǎn)行:unPIVOT:
基本語(yǔ)法:
create table #table1 (id int, code varchar(10), name1 varchar(20), name2 varchar(20), name3 varchar(20)); go insert into #table1(id, name1, name2, code, name3) values(1, 'm1', 'a1', 'a2', 'a3'), (2, 'm2', 'b1', 'b2', 'b3'), (4, 'm1', 'c1', 'c2', 'c3'); go select * from #table1; --方法一 select PVT.id, PVT.code, PVT.name, PVT.val from #table1 unpivot(val for name in(name1, name2, name3)) as PVT; --方法二 with P as (select * from #table1) select PVT.id, PVT.code, PVT.name, PVT.val from P unpivot(val for name in(name1, name2, name3)) as PVT; drop table #table1;
結(jié)果:
實(shí)例:
到此這篇關(guān)于SQL Server使用PIVOT與unPIVOT實(shí)現(xiàn)行列轉(zhuǎn)換的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SQL語(yǔ)句實(shí)現(xiàn)查詢并自動(dòng)創(chuàng)建Missing Index
這篇文章主要介紹了SQL語(yǔ)句實(shí)現(xiàn)查詢并自動(dòng)創(chuàng)建Missing Index,本文直接給出SQL實(shí)現(xiàn)腳本,需要的朋友可以參考下2015-07-07利用SQL SERVER建立登錄WINDOWS帳號(hào)
SQL SERVER建立WINDOWS登錄帳號(hào)2009-02-02SQL Server實(shí)現(xiàn)將特定字符串拆分并進(jìn)行插入操作的方法
這篇文章主要介紹了SQL Server實(shí)現(xiàn)將特定字符串拆分并進(jìn)行插入操作的方法,涉及SQL Server的循環(huán)、遍歷、判定及插入等相關(guān)操作技巧,需要的朋友可以參考下2016-08-08SQL Server誤區(qū)30日談 第28天 有關(guān)大容量事務(wù)日志恢復(fù)模式的誤區(qū)
在大容量事務(wù)日志恢復(fù)模式下只有一小部分批量操作可以被“最小記錄日志”,這類操作的列表可以在Operations That Can Be Minimally Logged找到。這是適合SQL Server 2008的列表,對(duì)于不同的SQL Server版本,請(qǐng)確保查看正確的列表2013-01-01MS-SQL Server 中單引號(hào)的兩種處理方法
MS-SQL Server 中單引號(hào)的兩種處理方法...2007-01-01SQL語(yǔ)句去掉重復(fù)記錄,獲取重復(fù)記錄
SQL語(yǔ)句去掉重復(fù)記錄,獲取重復(fù)記錄...2007-03-03SQL Server自動(dòng)生成日期加數(shù)字的序列號(hào)
需要生成下面的序列號(hào),前半部分是yyyymmdd格式的年月日時(shí)間數(shù)字,后半部分則是每天都從1順序增長(zhǎng)的數(shù)字,位數(shù)要固定,中間不足的補(bǔ)0。2009-08-08sql 查詢本年、本月、本日記錄的語(yǔ)句,附SQL日期函數(shù)
sql 查詢本年、本月、本日記錄的語(yǔ)句,附SQL日期函數(shù),學(xué)習(xí)sql的朋友可以參考下。2011-07-07