PIVOT 用于將列值旋轉(zhuǎn)為列名(即行轉(zhuǎn)列),在 SQL Server 2000可以用聚合函數(shù)配合CASE語句實現(xiàn)
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:主機域名、網(wǎng)站空間、營銷軟件、網(wǎng)站建設(shè)、儀征網(wǎng)站維護、網(wǎng)站推廣。
PIVOT 的一般語法是:PIVOT(聚合函數(shù)(列) FOR 列 in (…) )AS P
注意:PIVOT、UNPIVOT是SQL Server 2005 的語法,使用需修改數(shù)據(jù)庫兼容級別(在數(shù)據(jù)庫屬性-選項-兼容級別改為 90 )
SQL2008 中可以直接使用
完整語法:
table_source
PIVOT(
聚合函數(shù)(value_column)
FOR pivot_column
IN(column_list)
)
View Code
UNPIVOT 用于將列明轉(zhuǎn)為列值(即列轉(zhuǎn)行),在SQL Server 2000可以用UNION來實現(xiàn)
完整語法:
table_source
UNPIVOT(
value_column
FOR pivot_column
IN(column_list)
)
sql server 請參閱下面的代碼,列轉(zhuǎn)行
sqlserver列轉(zhuǎn)行方法分享
以下例子適用 sqlserver 2005版本及以上
create?table?A?(?info1?varchar(30),
[2012]?int,
[2008]?int,
[2018]?int,
[2013]?int
)
go
insert?into?A?values('A',8,null,null,20)
insert?into?A?values('B',null,7,null,3)
insert?into?A?values('C',12,4,null,null)
insert?into?A?values('D',null,null,5,16)
go
---列行轉(zhuǎn)換?適用于sql?server?2005及以上版本
SELECT?info1,nian,qty
from?A
unpivot(qty?for?nian?in([2012],[2008],[2018],[2013]))as?test
GO
truncate?table?A
drop?table?A
create table rotatetable1 (序號 int,company char(66),box_weight char(12),廢塑料numeric(10,2)),廢五金 numeric(10,2)),廢鋼鐵 numeric(10,2)),廢紙 numeric(10,2)),廢有色 numeric(10,2)),廢纖維 numeric(10,2)),其它 numeric(10,2)),合計 numeric(10,2)));
insert into rotatetable1(company,box_weight) select name ,'weight' from sum1 group by name;
insert into rotatetable1(company,box_weight) select name ,'box' from sum1 group by name;
update rotatetable1 set 廢塑料=box from sum1as a where a.name=rotatetable1.company and box_weight='box' and hsname='廢塑料';
update rotatetable1 set 廢塑料=weight from sum1as a where a.name=rotatetable1.company and box_weight='weight' and hsname='廢塑料';
::: :::
update rotatetable1 set 其它=box from sum1as a where a.name=rotatetable1.company and box_weight='box' and hsname='其它';
update rotatetable1 set 其它=weight from sum1as a where a.name=rotatetable1.company and box_weight='weight' and hsname='其它';
::: :::
update rotatetable1 set 合計=廢塑料+廢五金+廢鋼鐵+廢紙+廢有色+廢纖維+其它;
(所有涉及表的行列轉(zhuǎn)換均可按照這種方式實現(xiàn)。)
第一:你的時間維度表基本沒有意義,微軟SSIS中心認為時間維度至少由日期構(gòu)成主鍵??梢哉J為是最小基本業(yè)務(wù)顆粒。
來個Sample,更復(fù)雜的在我空間里。但是道理是一樣的。都是借助動態(tài)SQL和一些函數(shù)。
----------------------------------------------------------------
/*
作者:Edwin
數(shù)據(jù)庫:SQL?SERVER?2005+
作用:指定時間區(qū)間的自然周有幾天,如果垮年度,由外圍驗證
Version?1.0
Copyright?(c)?2015,?SQL?SERVER?2008
*/
----------------------------------------------------------------
/*參數(shù)設(shè)定區(qū)域,參數(shù)為開始時間和結(jié)束時間*/
----------------------------------------------------------------
declare?@FDate?DateTime?set?@FDate?=?'2014-09-01?00:00:00.000'
declare?@Edate?DateTime?set?@Edate?=?'2015-01-01?00:00:00.000'
----------------------------------------------------------------
/*SQL主體*/
----------------------------------------------------------------
declare?@WeekHeader?nvarchar(max)
select?@WeekHeader?=?coalesce(@WeekHeader+',['+cast(WeekOfYear?as?varchar)+']','['+cast(WeekOfYear?as?varchar)+?']')
from?
(
select?WeekOfYear?from?Comn.Calendar?where?DatePerDay=@FDate?and?DatePerDay@Edate?group?by?WeekOfYear?
)?M
declare?@PivotSQL?nvarchar(max)?set?@PivotSQL=N'
select
Year?as?年份,'+@WeekHeader+'
from
(
select?[Year],WeekOfYear,DatePerDay?from?[DT_WareHouse].[Comn].[Calendar]?where?DatePerDay=@FDate?and?DatePerDay@Edate
)?M
pivot
(
count(DatePerDay)?for?[WeekOfYear]?in('+@WeekHeader+')
)?PVT'
exec?sp_executesql?@PivotSQL,N'@FDate?datetime,@Edate?datetime',@FDate,@Edate
執(zhí)行結(jié)果:
在看一下時間維度表:
至于PVT標(biāo)題別名問題,這個可以在時間維度表中創(chuàng)建字符串類型的第幾周等樣式的列來完成。
這種方式比較簡單。
或是
----------------------------------------------------------------
/*
作者:Edwin
數(shù)據(jù)庫:SQL?SERVER?2005+
作用:指定時間區(qū)間的自然周銷售,統(tǒng)計周期為某一年,如果垮年度,由外圍驗證
Version?1.0
Copyright?(c)?2015,?SQL?SERVER?2008
*/
----------------------------------------------------------------
/*參數(shù)設(shè)定區(qū)域,參數(shù)為開始時間和結(jié)束時間*/
----------------------------------------------------------------
declare?@FDate?DateTime?set?@FDate?=?'2014-09-01?00:00:00.000'
declare?@Edate?DateTime?set?@Edate?=?'2015-01-01?00:00:00.000'
----------------------------------------------------------------
/*SQL主體*/
----------------------------------------------------------------
declare?@WeekHeader?nvarchar(max)
select?@WeekHeader?=?coalesce(@WeekHeader+',['+cast(WeekOfYear?as?varchar)+']','['+cast(WeekOfYear?as?varchar)+?']')
from?
(
select?('第'+cast(WeekOfYear?as?varchar)+'周')?as?WeekOfYear?from?Comn.Calendar?where?DatePerDay=@FDate?and?DatePerDay@Edate?group?by?WeekOfYear?
)?M
declare?@PivotSQL?nvarchar(max)?set?@PivotSQL=N'
select
Year?as?年份,'+@WeekHeader+'
from
(
select?[Year],(''第''+cast(WeekOfYear?as?varchar)+''周'')?as?WeekOfYear,DatePerDay?from?[DT_WareHouse].[Comn].[Calendar]?where?DatePerDay=@FDate?and?DatePerDay@Edate
)?M
pivot
(
count(DatePerDay)?for?[WeekOfYear]?in('+@WeekHeader+')
)?PVT'
exec?sp_executesql?@PivotSQL,N'@FDate?datetime,@Edate?datetime',@FDate,@Edate
結(jié)果: