日期:2014-05-18 浏览次数:20569 次
----------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2012-07-19 09:34:51
-- Version:
-- Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (Intel X86)
-- Apr 22 2011 11:57:00
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition on Windows NT 6.1 <X64> (Build 7600: ) (WOW64)
--
----------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([名称] varchar(4),[类型] varchar(1),[金额] int)
insert [tb]
select '张三','A',10 union all
select '张三','B',11 union all
select '张三','C',12 union all
select '张三','C',13 union all
select '张三','D',14 union all
select '李四','A',15
--------------开始查询--------------------------
select * from [tb] pivot (max(金额) for 类型 in (a,b,c,d)) b
----------------结果----------------------------
/* 名称 a b c d
---- ----------- ----------- ----------- -----------
李四 15 NULL NULL NULL
张三 10 11 13 14
(2 行受影响)
*/