declare @s nvarchar(4000)
set @s=''
select @s=@s+','+quotename(编号)
+'=isnull(max(case 编号 when '+rtrim(编号)
+' then 值 end),'''')'
from A
group by 编号
exec('select 类型'+@s+' from A group by 类型')
set @s=''
select @s=@s+','+quotename(编号)
+'=isnull(max(case 编号 when '+rtrim(编号)
+' then 值 end),'''')'
from A
group by 编号
exec('select 类型'+@s+' from A group by 类型')
解决方案 »
- 自增字段快达到类型极限了,如何清理自增类型字段?
- [求助 请问 SQL Server 2000 怎样在 Windows Server 2003 SP1 中安装????
- 急!!!大家帮帮忙,给介绍个网址之类的,关于介绍sql教本相关内容。 我要用sql 语句来建表 ,给表增加字段。
- SQL问题,请求解答。
- 请问如何写条件语句字段为datetime类型的sql,在线等待
- 求一条关于部分匹配检索的SQL语句
- 如何去除一个字段的连续空格?
- 问一个查询重复项的sql语句
- 如何在企业管理器里打开一张表的时候希望记录按某一字段的顺序排列显示
- fastreport中列宽固定,根据字段内容多少自动调整行高?
- sql server出错急!!!!
- 求一个简单的sql!
create table A(类型 varchar(10),编号 int,值 varchar(10))
insert A select 'A',1,'a'
union all select 'A',2,'b'
union all select 'A',3,'c'
union all select 'B',2,'d'
union all select 'B',3,'e'
union all select 'B',4,'f'
go--查询
declare @s nvarchar(4000)
set @s=''
select @s=@s+','+quotename(编号)
+'=isnull(max(case 编号 when '+rtrim(编号)
+' then 值 end),'''')'
from A
group by 编号
exec('select 类型'+@s+' from A group by 类型')
go--删除测试
drop table A/*--结果
类型 1 2 3 4
---------- ---------- ---------- ---------- ----------
A a b c
B d e f
--*/
,[1]=isnull(max(case 编号 when 1 then 值 end),'')
,[2]=isnull(max(case 编号 when 2 then 值 end),'')
,[3]=isnull(max(case 编号 when 3 then 值 end),'')
,[2]=isnull(max(case 编号 when 2 then 值 end),'')
,[3]=isnull(max(case 编号 when 3 then 值 end),'')
,[4]=isnull(max(case 编号 when 4 then 值 end),'')
这些是什么意思能解释一下吗?
有这么多列会什么最后就只有1,2,3,4这四列了啊???
@s应该为,[1]=isnull(max(case 编号 when 1 then 值 end),'')
,[2]=isnull(max(case 编号 when 2 then 值 end),'')
,[3]=isnull(max(case 编号 when 3 then 值 end),'')
,[2]=isnull(max(case 编号 when 2 then 值 end),'')
,[3]=isnull(max(case 编号 when 3 then 值 end),'')
,[4]=isnull(max(case 编号 when 4 then 值 end),'')我直接写出来为什么结果集出来不对啊,语句如下;
select 类型,[1]=isnull(max(case 编号 when 1 then 值 end),'')
,[2]=isnull(max(case 编号 when 2 then 值 end),'')
,[3]=isnull(max(case 编号 when 3 then 值 end),'')
,[2]=isnull(max(case 编号 when 2 then 值 end),'')
,[3]=isnull(max(case 编号 when 3 then 值 end),'')
,[4]=isnull(max(case 编号 when 4 then 值 end),'')
from A group by 类型
结果集为
A a b c b c
B d e d e f