SQL 2005 Pivot应用

Hans_wu 2008-03-26 05:09:35
有一表如下:
create table tb(ID varchar(50),CName varchar(50),Amount money,ActionDate datetime)
insert into tb values('001','a',2000.00,'2008-02-28')
insert into tb values('001','a',1560.00,'2008-03-28')
insert into tb values('001','a',2040.00,'2008-01-28')
insert into tb values('001','a',1003.00,'2008-04-28')
insert into tb values('101','b',3100.00,'2008-03-28')
insert into tb values('101','b',4040.00,'2008-01-28')
insert into tb values('101','b',5003.00,'2008-04-28')
insert into tb values('101','b',2400.00,'2008-02-28')
insert into tb values('001','a',15000.00,'2007-12-21')
insert into tb values('101','b',22400.00,'2007-12-21')
---------要将数据转换成如下
ID CName 2007-12 2008-01 2008-02 2008-03 2008-04
----------------------- --------------------- --------------------- --------------------- ---------------------
001 a 15000.00 2040.00 2000.00 1560.00 1003.00
101 b 22400.00 4040.00 2400.00 3100.00 5003.00

实现如下:
---------------Step 1
select ID,CName,sum(amount) as amount,left(convert(varchar(20),Actiondate,120),7) as Actiondate
into #tmp
from tb
group by ID,CName,left(convert(varchar(20),Actiondate,120),7)
--------Step2
declare @sql varchar(max), @sqlAll nvarchar(max)
select @sql=''
select @sql=@sql+'['+Actiondate+'],'
from #tmp group by Actiondate
select @sql=left(@sql,len(@sql)-1)
print @sql
---------Step3
select @sqlAll='
select * from #tmp s
pivot
(sum(amount)
for Actiondate in ('+@sql+')
) as P order by ID,CName '
print @sqlAll
------Step4
exec sp_executesql @sqlAll
drop table #tmp

...全文
2971 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
center182 2011-06-11
  • 打赏
  • 举报
回复
使用pivot的确将行转成了列,但能不能把
select ID,CName,sum(amount) as amount,left(convert(varchar(20),Actiondate,120),7) as Actiondate
into #tmp
from tb
group by ID,CName,left(convert(varchar(20),Actiondate,120),7)
--------Step2
declare @sql varchar(max), @sqlAll nvarchar(max)
select @sql=''
select @sql=@sql+'['+Actiondate+'],'
from #tmp group by Actiondate
select @sql=left(@sql,len(@sql)-1)
print @sql
---------Step3
select @sqlAll='
select * from #tmp s
pivot
(sum(amount)
for Actiondate in ('+@sql+')
) as P order by ID,CName '
print @sqlAll
------Step4
exec sp_executesql @sqlAll
放在一个select * from ()中呢??
还有就是没有列名,既然是动态的,怎么使用列名
chunling0922 2008-05-09
  • 打赏
  • 举报
回复
转贴:

SQL Server2005引入了很多迎合开发者口味的新特性,虽然改动不大,却大大了减少了开发者的工作量,这种替用户考虑的开发思路,值得称赞。

在SQL Server2000中,要实现行列转换,需要综合利用聚合函数和动态SQL,实现起来需要一定的技巧,所以在CSDN的SQL讨论区里可以看到大量询问行列转换如何实现的问题。到了2005中,使用新引进的关键字PIVOT/UNPIVOT,可以轻松实现行列转换的需求。

好像Oracle11g也准备引入PIVOT/UNPIVOT特性,对于Oracle开发来说,It''s a good news。

本文通过两个简单的例子展示PIVOT/UNPIVOT的用法。详细的语法请参考联机帮助。



PIVOT

创建测试表,插入测试数据

create table test(id int,name varchar(20),quarter int,profile int)
insert into test values(1,''a'',1,1000)
insert into test values(1,''a'',2,2000)
insert into test values(1,''a'',3,4000)
insert into test values(1,''a'',4,5000)
insert into test values(2,''b'',1,3000)
insert into test values(2,''b'',2,3500)
insert into test values(2,''b'',3,4200)
insert into test values(2,''b'',4,5500)

select * from test
id name quarter profile
----------- -------------------- ----------- -----------
1 a 1 1000
1 a 2 2000
1 a 3 4000
1 a 4 5000
2 b 1 3000
2 b 2 3500
2 b 3 4200
2 b 4 5500

(8 row(s) affected)

利用PIVOT将个季度的利润转成横向显示:

select id,name,
[1] as "一季度",
[2] as "二季度",
[3] as "三季度",
[4] as "四季度"
from
test
pivot
(
sum(profile)
for quarter in
([1],[2],[3],[4])
)
as pvt

id name 一季度 二季度 三季度 四季度
----------- -------------------- ----------- ----------- ----------- -----------
1 a 1000 2000 4000 5000
2 b 3000 3500 4200 5500

(2 row(s) affected)

UNPIVOT

建立测试表,插入测试数据

drop table test

create table test(id int,name varchar(20), Q1 int, Q2 int, Q3 int, Q4 int)

insert into test values(1,''a'',1000,2000,4000,5000)
insert into test values(2,''b'',3000,3500,4200,5500)


select * from test

id name Q1 Q2 Q3 Q4
----------- -------------------- ----------- ----------- ----------- -----------
1 a 1000 2000 4000 5000
2 b 3000 3500 4200 5500

(2 row(s) affected)

利用UNPIVOT,将同一行中四个季度的列数据转换成四行数据:

select id,name,quarter,profile
from
test
unpivot
(
profile
for quarter in
([Q1],[Q2],[Q3],[Q4])
)
as unpvt

id name quarter profile
----------- -------------------- ---------- -----------
1 a Q1 1000
1 a Q2 2000
1 a Q3 4000
1 a Q4 5000
2 b Q1 3000
2 b Q2 3500
2 b Q3 4200
2 b Q4 5500

(8 row(s) affected)

yanm7788 2008-05-07
  • 打赏
  • 举报
回复
有没有pivot更详细点的介绍啊。
lxuan_025 2008-05-06
  • 打赏
  • 举报
回复
有错误 运行不了
lxuan_025 2008-05-06
  • 打赏
  • 举报
回复
是个例子,可以研究以下
cgsun 2008-04-20
  • 打赏
  • 举报
回复
如果能有说明就更好了。
hery2002 2008-04-01
  • 打赏
  • 举报
回复
Mark
Limpire 2008-04-01
  • 打赏
  • 举报
回复
支持一下

6,129

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 新技术前沿
社区管理员
  • 新技术前沿社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧