有一个SQL查出来
select a,y,v from table1查出来的值数
a y v
产品一 2001 4
产品一 2003 5
产品二 2001 6
产品二 2002 6sql查询出来值如何变为以下值a 2001 2002 2003
产品一 4 NULL 5
产品二 6 6 NULL
select a,y,v from table1查出来的值数
a y v
产品一 2001 4
产品一 2003 5
产品二 2001 6
产品二 2002 6sql查询出来值如何变为以下值a 2001 2002 2003
产品一 4 NULL 5
产品二 6 6 NULL
a,
max(case y when '2001' then v else '' end) as '2001',
max(case y when '2002' then v else '' end) as '2002',
max(case y when '2003' then v else '' end) as '2003'
from
tb
group by
a
-- Author :fredrickhu(小F,向高手学习)
-- Date :2010-11-07 23:10:47
-- 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.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([a] varchar(6),[y] int,[v] int)
insert [tb]
select '产品一',2001,4 union all
select '产品一',2003,5 union all
select '产品二',2001,6 union all
select '产品二',2002,6
--------------开始查询--------------------------
select
a,
max(case y when ltrim(2001) then ltrim(v) else null end) as '2001',
max(case y when ltrim(2002) then ltrim(v) else null end) as '2002',
max(case y when ltrim(2003) then ltrim(v) else null end) as '2003'
from
tb
group by
a----------------结果----------------------------
/* a 2001 2002 2003
------ ------------ ------------ ------------
产品二 6 6 NULL
产品一 4 NULL 5
警告: 聚合或其他 SET 操作消除了空值。(2 行受影响)*/
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([a] varchar(6),[y] int,[v] int)
insert [tb]
select '产品一',2001,4 union all
select '产品一',2003,5 union all
select '产品二',2001,6 union all
select '产品二',2002,6
--------------开始查询--------------------------
SELECT * FROM [tb] AS T
PIVOT(SUM([v]) FOR [y] IN ([2001],[2002],[2003])) AS P
GO
insert [tb]
select '产品一',2001,4 union all
select '产品一',2003,5 union all
select '产品二',2001,6 union all
select '产品二',2002,6--sql 2000静态,指你能确定Y为2001-2003。
select a,
max(case y when 2001 then v else null end) [2001],
max(case y when 2002 then v else null end) [2002],
max(case y when 2003 then v else null end) [2003]
from tb
group by a
/*
a 2001 2002 2003
------ ----------- ----------- -----------
产品二 6 6 NULL
产品一 4 NULL 5(所影响的行数为 2 行)
*/--sql 2000动态,指你不能确定Y的值。
declare @sql varchar(8000)
set @sql = 'select a '
select @sql = @sql + ' , max(case y when ''' + cast(y as varchar) + ''' then v else null end) [' + cast(y as varchar) + ']'
from (select distinct y from tb) as t
set @sql = @sql + ' from tb group by a'
exec(@sql)
/*
a 2001 2002 2003
------ ----------- ----------- -----------
产品二 6 6 NULL
产品一 4 NULL 5(所影响的行数为 2 行)
*/drop table tb
insert [tb]
select N'产品一',2001,4 union all
select N'产品一',2003,5 union all
select N'产品二',2001,6 union all
select N'产品二',2002,6--sql 2005静态,指你能确定Y为2001-2003。
select * from tb a pivot (max(v) for y in ([2001],[2002],[2003])) b
/*
a 2001 2002 2003
------ ----------- ----------- -----------
产品一 4 NULL 5
产品二 6 6 NULL(2 行受影响)
*/--sql 2005动态,指你不能确定Y的值。
declare @sql nvarchar(4000)
select @sql = isnull(@sql + '],[' , '') + cast(y as varchar) from tb group by cast(y as varchar)
set @sql = '[' + @sql + ']'
exec ('select * from (select * from tb) a pivot (max(v) for y in (' + @sql + ')) b')
/*
a 2001 2002 2003
------ ----------- ----------- -----------
产品一 4 NULL 5
产品二 6 6 NULL(2 行受影响)
*/drop table tb