34,576
社区成员
发帖
与我相关
我的任务
分享
联机丛书上的例子
USE AdventureWorks;
GO
SELECT VendorID, [164] AS Emp1, [198] AS Emp2, [223] AS Emp3, [231] AS Emp4, [233] AS Emp5
FROM
(SELECT PurchaseOrderID, EmployeeID, VendorID
FROM Purchasing.PurchaseOrderHeader) p
PIVOT
(
COUNT (PurchaseOrderID)
FOR EmployeeID IN
( [164], [198], [223], [231], [233] )
) AS pvt
ORDER BY VendorID
----------------------------------------------------------------
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-09-14 09:11:27
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([name] varchar(1),[f1] varchar(3),[f2] int)
insert [tb]
select 'a','aaa',1 union all
select 'b','bbb',2 union all
select 'a','aaa',2 union all
select 'b','ccc',1
--------------开始查询--------------------------
select
[name], [1],[2]
from
[tb]
pivot
(
max([f1])
for [f2]
in
([1],[2])
)
as f
----------------结果----------------------------
/* name 1 2
---- ---- ----
a aaa aaa
b ccc bbb
(2 行受影响)
*/
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([name] varchar(1),[f1] varchar(3),[f2] int)
insert [tb]
select 'a','aaa',1 union all
select 'b','bbb',2 union all
select 'a','aaa',2 union all
select 'b','ccc',1
select [name],
max([f1])[1],
min([f1])[2]
from tb group by [name]
/*
name 1 2
---- ---- ----
a aaa aaa
b ccc bbb
(2 個資料列受到影響)
*/
--> 生成测试数据表:tb
IF NOT OBJECT_ID('[tb]') IS NULL
DROP TABLE [tb]
GO
CREATE TABLE [tb]([name] NVARCHAR(10),[f1] NVARCHAR(10),[f2] INT)
INSERT [tb]
SELECT 'a','aaa',1 UNION ALL
SELECT 'b','bbb',2 UNION ALL
SELECT 'a','aaa',2 UNION ALL
SELECT 'b','ccc',1
GO
--SELECT * FROM [tb]
-->SQL查询如下:
SELECT *
FROM tb
PIVOT(MAX(f1) FOR f2 IN([1],[2]))b
/*
name 1 2
---------- ---------- ----------
a aaa aaa
b ccc bbb
*/
------------------------------------------------
-----------------SQL 版本---------------------
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
---------------2009-09-14 01:01:34-----------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([name] varchar(1),[f1] varchar(3),[f2] int)
insert [tb]
select 'a','aaa',1 union all
select 'b','bbb',2 union all
select 'a','aaa',2 union all
select 'b','ccc',1
select [name], [1],[2] from [tb]
pivot
(
max([f1])
for [f2] in
([1],[2])
)
as pvt
-----------------结果-------------------
/*name 1 2
---- ---- ----
a aaa aaa
b ccc bbb
(2 行受影响)
*/
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