【转】Sql server 2005 中的列置换函数用法(pivot 字段)
原文链接:http://blog.csdn.net/zlp321002/archive/2007/04/02/1549484.aspx
行列置换(2000中的交叉表.2005中的处理方法.及相关合计字段列的处理.)
declare @t table(name varchar(10),object varchar(10),score int)
insert into @t select 'a','EN',89
union all select 'a','CH',78
union all select 'a','HO',99
union all select 'b','EN',34
union all select 'b','CH',88
union all select 'b','HO',66
select ta.*,tb.[sum] from
(
select * from @t
pivot
(sum(score)
for object in ([EN],[CH],[HO])
)
as pt
)ta left join
(
select name,[sum]=sum(score)
from @t
group by name
)tb
on ta.name=tb.name
/*
name EN CH HO sum
---------- ----------- ----------- ----------- -----------
a 89 78 99 266
b 34 88 66 188
(2 行受影响)
*/
本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利
This posting is provided "AS IS" with no warranties, and confers no rights.
This posting is provided "AS IS" with no warranties, and confers no rights.

浙公网安备 33010602011771号