create table temp(学号 int,姓名 varchar(10),时间 datetime ,实验结果 varchar(10)) insert into temp select 4,'王五',getdate(),'通过'select * from temp drop function hb create function hb(@name varchar(10)) returns varchar(8000) as begin declare @s varchar(8000) set @s='' select @s=@s+','+ 实验结果 from temp where 姓名=@name return right(@s,len(@s) - 1) endselect min(学号)as 学号, 姓名,dbo.hb(姓名) from temp group by 姓名 order by 学号1 张三 通过,通过 2 李四 通过 3 王五 失败,失败,通过 --记录我插入完删了 soryy
--你看这样行不行declare @t table (xh int,name varchar(20),sj varchar(40),jg varchar(20)) insert into @t select 01,'张三','2007-12-13 08:21:52.010','通过' union all select 01,'张三','2007-12-13 08:21:53.010','通过' union all select 02,'李四','2007-12-13 08:21:54.010','通过' union all select 03,'王五','2007-12-13 08:21:55.010','失败' union all select 03,'王五','2007-12-13 08:21:56.010','失败' union all select 03,'王五','2007-12-13 08:21:57.010','通过' select xh,name,max(case when px=1 then jg else ''end) sy1, max(case when px=2 then jg else ''end) sy2, max(case when px=3 then jg else ''end) sy3 from (select a.*,px=(select count(1)+1 from @t where xh=a.xh and a.sj>sj) from @t a) n group by xh,name order by xh
create table T(学号 varchar(2),姓名 nvarchar(5),时间 datetime,实验结果 nvarchar(10)) insert T select '01','张三','2007-12-10','通过' insert T select '01','张三','2007-12-11','通过' insert T select '02','李四','2007-12-10','通过' insert T select '03','王五','2007-12-11','失败' insert T select '03','王五','2007-12-12','失败' insert T select '03','王五','2007-12-13','通过' godeclare @i int,@s nvarchar(4000) select @i=max(con) from (select con=count(1) from T group by 学号)TT set @s='' while @i!<1 select @s=',[实验'+rtrim(@i)+']=max(case when con='+rtrim(@i)+' then [实验结果] else '''' end)'+@s,@i=@i-1exec('select 学号,姓名'+@s+' from (select *,con=(select count(1) from T where 学号=a.学号 and 时间!>a.时间 ) from T a)Tmp group by 学号,姓名') 学号 姓名 实验1 实验2 实验3 ---- ----- ---------- ---------- ---------- 02 李四 通过 03 王五 失败 失败 通过 01 张三 通过 通过
--加个排序: declare @i int,@s nvarchar(4000) select @i=max(con) from (select con=count(1) from T group by 学号)TT set @s='' while @i!<1 select @s=',[实验'+rtrim(@i)+']=max(case when con='+rtrim(@i)+' then [实验结果] else '''' end)'+@s,@i=@i-1exec('select 学号,姓名'+@s+' from (select *,con=(select count(1) from T where 学号=a.学号 and 时间!>a.时间 ) from T a)Tmp group by 学号,姓名 order by 学号 ')学号 姓名 实验1 实验2 实验3 ---- ----- ---------- ---------- ---------- 01 张三 通过 通过 02 李四 通过 03 王五 失败 失败 通过 ---- 生成的语句如下: select 学号,姓名,[实验1]=max(case when con=1 then [实验结果] else '' end),[实验2]=max(case when con=2 then [实验结果] else '' end),[实验3]=max(case when con=3 then [实验结果] else '' end) from (select *,con=(select count(1) from T where 学号=a.学号 and 时间!>a.时间 ) from T a)Tmp group by 学号,姓名 order by 学号
时间(没有大小关系时需要生成临时表) select *,con=0 into # from Tgo declare @a nvarchar(2),@b int update # set @b=case when 学号=@a then @b+1 else 1 end,con=@b,@a=学号 ------ 生成con的递增记录数
create table tb(学号 varchar(10),姓名 varchar(10),实验结果 varchar(10)) insert into tb values('01','张三','通过') insert into tb values('01','张三','通过') insert into tb values('02','李四','通过') insert into tb values('03','王五','失败') insert into tb values('03','王五','失败') insert into tb values('04','王五','通过') go--创建一个合并的函数 create function f_hb(@姓名 varchar(10)) returns varchar(8000) as begin declare @str varchar(8000) set @str = '' select @str = @str + ',' + cast(实验结果 as varchar) from tb where 姓名 = @姓名 set @str = right(@str , len(@str) - 1) return(@str) End go--调用自定义函数得到结果:select m.学号 , m.姓名 , n.实验结果 from (select 姓名 , min(学号) 学号 from tb group by 姓名) m, (select distinct 姓名 ,dbo.f_hb(姓名) as 实验结果 from tb) n where m.姓名 = n.姓名 order by 学号drop table tb drop function f_hb/* 学号 姓名 实验结果 ---------- ---------- ------------- 01 张三 通过,通过 02 李四 通过 03 王五 失败,失败,通过(所影响的行数为 3 行) */
create table tb(学号 varchar(10),姓名 varchar(10),时间 datetime , 实验结果 varchar(10)) insert into tb values('01','张三','2007-01-01','通过') insert into tb values('01','张三','2007-01-02','通过') insert into tb values('02','李四','2007-01-03','通过') insert into tb values('03','王五','2007-01-04','失败') insert into tb values('03','王五','2007-01-05','失败') insert into tb values('03','王五','2007-01-06','通过') goselect 学号 ,姓名 , max(case px when 1 then 实验结果 else '' end) '试验1', max(case px when 2 then 实验结果 else '' end) '试验2', max(case px when 3 then 实验结果 else '' end) '试验3' from ( select px=(select count(1) from tb where 姓名=a.姓名 and 时间<a.时间)+1 , * from tb a ) t group by 学号 ,姓名 order by 学号drop table tb/* 学号 姓名 试验1 试验2 试验3 ---------- ---------- ---------- ---------- ---------- 01 张三 通过 通过 02 李四 通过 03 王五 失败 失败 通过(所影响的行数为 3 行) */
create table tb(学号 varchar(10),姓名 varchar(10),时间 datetime , 实验结果 varchar(10)) insert into tb values('01','张三','2007-01-01','通过') insert into tb values('01','张三','2007-01-02','通过') insert into tb values('02','李四','2007-01-03','通过') insert into tb values('03','王五','2007-01-04','失败') insert into tb values('03','王五','2007-01-05','失败') insert into tb values('03','王五','2007-01-06','通过') go--静态SQL,指实验数量最大为三次 select 学号 ,姓名 , max(case px when 1 then 实验结果 else '' end) '试验1', max(case px when 2 then 实验结果 else '' end) '试验2', max(case px when 3 then 实验结果 else '' end) '试验3' from ( select px=(select count(1) from tb where 姓名=a.姓名 and 时间<a.时间)+1 , * from tb a ) t group by 学号 ,姓名 order by 学号 /* 学号 姓名 试验1 试验2 试验3 ---------- ---------- ---------- ---------- ---------- 01 张三 通过 通过 02 李四 通过 03 王五 失败 失败 通过(所影响的行数为 3 行) */--动态SQL,指实验数量最大次数未知 declare @sql varchar(8000) set @sql = 'select 学号 ,姓名' select @sql = @sql + ' , max(case px when ''' + cast(px as varchar) + ''' then 实验结果 else '' '' end) [实验' + cast(px as varchar) + ']' from (select distinct px from (select px=(select count(1) from tb where 姓名=a.姓名 and 时间<a.时间)+1 , * from tb a) t) as a set @sql = @sql + ' from (select px=(select count(1) from tb where 姓名=a.姓名 and 时间<a.时间)+1 , * from tb a) t group by 学号 ,姓名 order by 学号' exec(@sql) /* 学号 姓名 实验1 实验2 实验3 ---------- ---------- ---------- ---------- ---------- 01 张三 通过 通过 02 李四 通过 03 王五 失败 失败 通过*/drop table tb
insert into temp select 4,'王五',getdate(),'通过'select * from temp
drop function hb
create function hb(@name varchar(10))
returns varchar(8000)
as
begin
declare @s varchar(8000)
set @s=''
select @s=@s+','+ 实验结果 from temp where 姓名=@name
return right(@s,len(@s) - 1)
endselect min(学号)as 学号, 姓名,dbo.hb(姓名) from temp group by 姓名 order by 学号1 张三 通过,通过
2 李四 通过
3 王五 失败,失败,通过
--记录我插入完删了 soryy
--你看这样行不行declare @t table (xh int,name varchar(20),sj varchar(40),jg varchar(20))
insert into @t
select 01,'张三','2007-12-13 08:21:52.010','通过'
union all select 01,'张三','2007-12-13 08:21:53.010','通过'
union all select 02,'李四','2007-12-13 08:21:54.010','通过'
union all select 03,'王五','2007-12-13 08:21:55.010','失败'
union all select 03,'王五','2007-12-13 08:21:56.010','失败'
union all select 03,'王五','2007-12-13 08:21:57.010','通过'
select xh,name,max(case when px=1 then jg else ''end) sy1,
max(case when px=2 then jg else ''end) sy2,
max(case when px=3 then jg else ''end) sy3
from (select a.*,px=(select count(1)+1 from @t where xh=a.xh and a.sj>sj) from @t a) n group by xh,name order by xh
insert T select '01','张三','2007-12-10','通过'
insert T select '01','张三','2007-12-11','通过'
insert T select '02','李四','2007-12-10','通过'
insert T select '03','王五','2007-12-11','失败'
insert T select '03','王五','2007-12-12','失败'
insert T select '03','王五','2007-12-13','通过' godeclare @i int,@s nvarchar(4000)
select @i=max(con) from (select con=count(1) from T group by 学号)TT
set @s=''
while @i!<1
select @s=',[实验'+rtrim(@i)+']=max(case when con='+rtrim(@i)+' then [实验结果] else '''' end)'+@s,@i=@i-1exec('select 学号,姓名'+@s+' from
(select *,con=(select count(1) from T where 学号=a.学号 and 时间!>a.时间 ) from T a)Tmp
group by 学号,姓名')
学号 姓名 实验1 实验2 实验3
---- ----- ---------- ---------- ----------
02 李四 通过
03 王五 失败 失败 通过
01 张三 通过 通过
--加个排序:
declare @i int,@s nvarchar(4000)
select @i=max(con) from (select con=count(1) from T group by 学号)TT
set @s=''
while @i!<1
select @s=',[实验'+rtrim(@i)+']=max(case when con='+rtrim(@i)+' then [实验结果] else '''' end)'+@s,@i=@i-1exec('select 学号,姓名'+@s+' from
(select *,con=(select count(1) from T where 学号=a.学号 and 时间!>a.时间 ) from T a)Tmp
group by 学号,姓名 order by 学号 ')学号 姓名 实验1 实验2 实验3
---- ----- ---------- ---------- ----------
01 张三 通过 通过
02 李四 通过
03 王五 失败 失败 通过
----
生成的语句如下:
select 学号,姓名,[实验1]=max(case when con=1 then [实验结果] else '' end),[实验2]=max(case when con=2 then [实验结果] else '' end),[实验3]=max(case when con=3 then [实验结果] else '' end) from
(select *,con=(select count(1) from T where 学号=a.学号 and 时间!>a.时间 ) from T a)Tmp
group by 学号,姓名 order by 学号
select *,con=0 into # from Tgo
declare @a nvarchar(2),@b int
update #
set @b=case when 学号=@a then @b+1 else 1 end,con=@b,@a=学号
------
生成con的递增记录数
insert into tb values('01','张三','通过')
insert into tb values('01','张三','通过')
insert into tb values('02','李四','通过')
insert into tb values('03','王五','失败')
insert into tb values('03','王五','失败')
insert into tb values('04','王五','通过')
go--创建一个合并的函数
create function f_hb(@姓名 varchar(10))
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str = ''
select @str = @str + ',' + cast(实验结果 as varchar) from tb where 姓名 = @姓名
set @str = right(@str , len(@str) - 1)
return(@str)
End
go--调用自定义函数得到结果:select m.学号 , m.姓名 , n.实验结果 from
(select 姓名 , min(学号) 学号 from tb group by 姓名) m,
(select distinct 姓名 ,dbo.f_hb(姓名) as 实验结果 from tb) n
where m.姓名 = n.姓名
order by 学号drop table tb
drop function f_hb/*
学号 姓名 实验结果
---------- ---------- -------------
01 张三 通过,通过
02 李四 通过
03 王五 失败,失败,通过(所影响的行数为 3 行)
*/
insert into tb values('01','张三','2007-01-01','通过')
insert into tb values('01','张三','2007-01-02','通过')
insert into tb values('02','李四','2007-01-03','通过')
insert into tb values('03','王五','2007-01-04','失败')
insert into tb values('03','王五','2007-01-05','失败')
insert into tb values('03','王五','2007-01-06','通过')
goselect 学号 ,姓名 ,
max(case px when 1 then 实验结果 else '' end) '试验1',
max(case px when 2 then 实验结果 else '' end) '试验2',
max(case px when 3 then 实验结果 else '' end) '试验3'
from
(
select px=(select count(1) from tb where 姓名=a.姓名 and 时间<a.时间)+1 , * from tb a
) t
group by 学号 ,姓名
order by 学号drop table tb/*
学号 姓名 试验1 试验2 试验3
---------- ---------- ---------- ---------- ----------
01 张三 通过 通过
02 李四 通过
03 王五 失败 失败 通过(所影响的行数为 3 行)
*/
insert into tb values('01','张三','2007-01-01','通过')
insert into tb values('01','张三','2007-01-02','通过')
insert into tb values('02','李四','2007-01-03','通过')
insert into tb values('03','王五','2007-01-04','失败')
insert into tb values('03','王五','2007-01-05','失败')
insert into tb values('03','王五','2007-01-06','通过')
go--静态SQL,指实验数量最大为三次
select 学号 ,姓名 ,
max(case px when 1 then 实验结果 else '' end) '试验1',
max(case px when 2 then 实验结果 else '' end) '试验2',
max(case px when 3 then 实验结果 else '' end) '试验3'
from
(
select px=(select count(1) from tb where 姓名=a.姓名 and 时间<a.时间)+1 , * from tb a
) t
group by 学号 ,姓名
order by 学号
/*
学号 姓名 试验1 试验2 试验3
---------- ---------- ---------- ---------- ----------
01 张三 通过 通过
02 李四 通过
03 王五 失败 失败 通过(所影响的行数为 3 行)
*/--动态SQL,指实验数量最大次数未知
declare @sql varchar(8000)
set @sql = 'select 学号 ,姓名'
select @sql = @sql + ' , max(case px when ''' + cast(px as varchar) + ''' then 实验结果 else '' '' end) [实验' + cast(px as varchar) + ']'
from (select distinct px from (select px=(select count(1) from tb where 姓名=a.姓名 and 时间<a.时间)+1 , * from tb a) t) as a
set @sql = @sql + ' from (select px=(select count(1) from tb where 姓名=a.姓名 and 时间<a.时间)+1 , * from tb a) t group by 学号 ,姓名 order by 学号'
exec(@sql)
/*
学号 姓名 实验1 实验2 实验3
---------- ---------- ---------- ---------- ----------
01 张三 通过 通过
02 李四 通过
03 王五 失败 失败 通过*/drop table tb