select personname ,case when projectid='编号1' then taskcontent else '' end as 编号1 ,case when projectid='编号2' then taskcontent else '' end as 编号2 from table
---------------------------------------------------------------------------------- -- Author : htl258(Tony) -- Date : 2010-05-10 22:15:33 -- Version: Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) -- Jul 9 2008 14:43:34 -- Copyright (c) 1988-2008 Microsoft Corporation -- Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 2) -- Blog : http://blog.csdn.net/htl258 ------------------------------------------------------------------------------------> 生成测试数据表: [tb] IF OBJECT_ID('[tb]') IS NOT NULL DROP TABLE [tb] GO CREATE TABLE [tb] ([ProjectID] [nvarchar](10),[PersonName] [nvarchar](10),[TaskContent] [nvarchar](10)) INSERT INTO [tb] SELECT '编号1','张三','水晶' UNION ALL SELECT '编号2','李四','矿石' UNION ALL SELECT '编号1','王二','水晶' UNION ALL SELECT '编号2','麻子','矿石'--SELECT * FROM [tb]-->SQL查询如下: declare @s varchar(8000) select @s=isnull(@s,'select PersonName') +',max(case ProjectID when '''+ProjectID+''' then TaskContent else '''' end) ['+ProjectID+']' from tb group by ProjectID exec(@s+' from tb group by PersonName') /* PersonName 编号1 编号2 ---------- ---------- ---------- 李四 矿石 麻子 矿石 王二 水晶 张三 水晶 (4 行受影响) */自己到ACCESS区去转吧
--> 测试数据:[tb] if object_id('[tb]') is not null drop table [tb] go create table [tb]([id] varchar(5),[name] varchar(4),[content] varchar(4)) insert [tb] select '编号1','张三','水晶' union all select '编号2','李四','矿石' union all select '编号1','王二','水晶' union all select '编号2','麻子','矿石'--------------------------------查询开始------------------------------select [name], 编号1=case [id] when '编号1' then [content] else '' end, 编号2=case [id] when '编号2' then [content] else '' end from [tb] /* name 编号1 编号2 ---- ---- ---- 张三 水晶 李四 矿石 王二 水晶 麻子 矿石(4 行受影响) */
declare @s varchar(8000) select @s=isnull(@s,'select PersonName') +',case ProjectID when '''+ProjectID+''' then TaskContent else '''' end ['+ProjectID+']' from tb group by ProjectID exec(@s+' from tb ')PersonName 编号1 编号2 ---------- ---------- ---------- 张三 水晶 李四 矿石 王二 水晶 麻子 矿石(4 行受影响)
TRANSFORM max(TaskContent) SELECT PersonName FROM Table1 GROUP BY PersonName PIVOT ProjectID;
这位ACCESS高手现身了,没想到ACCESS竟然也有PIVOT,SQL到2005才有
CREATE TABLE [tb] ([ProjectID] [nvarchar](10),[PersonName] [nvarchar](10),[TaskContent] [nvarchar](10)) INSERT INTO [tb] SELECT '编号1','张三','水晶' UNION ALL SELECT '编号2','李四','矿石' UNION ALL SELECT '编号1','王二','水晶' UNION ALL SELECT '编号2','麻子','矿石'select PersonName, max(case TaskContent when '水晶' then '水晶' else '' end) 编号1, max(case TaskContent when '矿石' then '矿石' else '' end) 编号2 from tb group by PersonNamedrop table tb/* PersonName 编号1 编号2 ---------- ---- ---- 李四 矿石 麻子 矿石 王二 水晶 张三 水晶 (所影响的行数为 4 行) */
,case when projectid='编号1' then taskcontent else '' end as 编号1
,case when projectid='编号2' then taskcontent else '' end as 编号2
from table
-- Author : htl258(Tony)
-- Date : 2010-05-10 22:15:33
-- Version: Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 2)
-- Blog : http://blog.csdn.net/htl258
------------------------------------------------------------------------------------> 生成测试数据表: [tb]
IF OBJECT_ID('[tb]') IS NOT NULL
DROP TABLE [tb]
GO
CREATE TABLE [tb] ([ProjectID] [nvarchar](10),[PersonName] [nvarchar](10),[TaskContent] [nvarchar](10))
INSERT INTO [tb]
SELECT '编号1','张三','水晶' UNION ALL
SELECT '编号2','李四','矿石' UNION ALL
SELECT '编号1','王二','水晶' UNION ALL
SELECT '编号2','麻子','矿石'--SELECT * FROM [tb]-->SQL查询如下:
declare @s varchar(8000)
select @s=isnull(@s,'select PersonName')
+',max(case ProjectID when '''+ProjectID+''' then TaskContent else '''' end) ['+ProjectID+']'
from tb group by ProjectID
exec(@s+' from tb group by PersonName')
/*
PersonName 编号1 编号2
---------- ---------- ----------
李四 矿石
麻子 矿石
王二 水晶
张三 水晶 (4 行受影响)
*/自己到ACCESS区去转吧
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([id] varchar(5),[name] varchar(4),[content] varchar(4))
insert [tb]
select '编号1','张三','水晶' union all
select '编号2','李四','矿石' union all
select '编号1','王二','水晶' union all
select '编号2','麻子','矿石'--------------------------------查询开始------------------------------select [name],
编号1=case [id] when '编号1' then [content] else '' end,
编号2=case [id] when '编号2' then [content] else '' end
from [tb]
/*
name 编号1 编号2
---- ---- ----
张三 水晶
李四 矿石
王二 水晶
麻子 矿石(4 行受影响)
*/
select @s=isnull(@s,'select PersonName')
+',case ProjectID when '''+ProjectID+''' then TaskContent else '''' end ['+ProjectID+']'
from tb group by ProjectID
exec(@s+' from tb ')PersonName 编号1 编号2
---------- ---------- ----------
张三 水晶
李四 矿石
王二 水晶
麻子 矿石(4 行受影响)
SELECT PersonName
FROM Table1
GROUP BY PersonName
PIVOT ProjectID;
INSERT INTO [tb]
SELECT '编号1','张三','水晶' UNION ALL
SELECT '编号2','李四','矿石' UNION ALL
SELECT '编号1','王二','水晶' UNION ALL
SELECT '编号2','麻子','矿石'select PersonName,
max(case TaskContent when '水晶' then '水晶' else '' end) 编号1,
max(case TaskContent when '矿石' then '矿石' else '' end) 编号2
from tb
group by PersonNamedrop table tb/*
PersonName 编号1 编号2
---------- ---- ----
李四 矿石
麻子 矿石
王二 水晶
张三 水晶 (所影响的行数为 4 行)
*/