库结构是:姓名、次数、结果------------------
姓名 次数 结果
A 1 90
A 2 120
A 3 110
A 4 100
B 1 100
B 2 140现在想展示成
姓名 次数1 次数2 次数3 次数4 ……
A 90 120 110 100
B 100 140如何实现啊?谢谢
姓名 次数 结果
A 1 90
A 2 120
A 3 110
A 4 100
B 1 100
B 2 140现在想展示成
姓名 次数1 次数2 次数3 次数4 ……
A 90 120 110 100
B 100 140如何实现啊?谢谢
drop table tbTest
GO
create table tbTest(姓名 varchar(10),次数 int,结果 int)
insert tbTest
select 'A', 1, 90 union all
select 'A', 2, 120 union all
select 'A', 3, 110 union all
select 'A', 4, 100 union all
select 'B', 1, 100 union all
select 'B', 2, 140declare @sql varchar(8000)
set @sql = ''
select @sql = @sql + ',次数' + rtrim(次数) + '=sum(case 次数 when ' + rtrim(次数) + ' then 结果 end)'
from tbTest group by 次数 order by 次数
set @sql = 'select 姓名' + @sql + ' from tbTest group by 姓名'
print @sql
EXEC(@sql)drop table tbTest/*结果
姓名 次数1 次数2 次数3 次数4
--------------------------------------------
A 90 120 110 100
B 100 140
*/
insert testSS select 'A', 1 ,90
union all select 'A', 2, 120
union all select 'A', 3, 110
union all select 'A', 4, 100
union all select 'B', 1, 100
union all select 'B', 2, 140declare @s varchar(1000)select @s=coalesce(@s+',','')+' sum(case when 次数='+ltrim(次数) +' then 结果 end) [次数'+ltrim(次数)+']' from test group by 次数exec('select 姓名,'+@s+' from test group by 姓名')
,stimes int,recource float)
insert into #t
select 'A',1,90
union all select 'A',2,120
union all select 'A',3,110
union all select 'A',4,100
union all select 'B',1,100
union all select 'B',2,140
go
select * from #tdeclare @sql varchar(8000)
set @sql = ''
select @sql = @sql + ',stime' + cast(stimes as varchar(3)) + '=sum(case stimes when ' + cast(stimes as varchar(3))+ ' then recource end)'
from #t group by stimes order by stimes
set @sql = 'select sname' + @sql + ' from #t group by sname'
print @sqlexec(@sql)drop table #t
(影響 6 個資料列)sname stimes recource
-------------------- ----------- -----------------------------------------------------
A 1 90.0
A 2 120.0
A 3 110.0
A 4 100.0
B 1 100.0
B 2 140.0(影響 6 個資料列)select sname,stime1=sum(case stimes when 1 then recource end),stime2=sum(case stimes when 2 then recource end),stime3=sum(case stimes when 3 then recource end),stime4=sum(case stimes when 4 then recource end) from #t group by sname
sname stime1 stime2 stime3 stime4
-------------------- ----------------------------------------------------- ----------------------------------------------------- ----------------------------------------------------- -----------------------------------------------------
A 90.0 120.0 110.0 100.0
B 100.0 140.0 NULL NULL
insert tbTest
select 'A', 1, 90 union all
select 'A', 2, 120 union all
select 'A', 3, 110 union all
select 'A', 4, 100 union all
select 'B', 1, 100 union all
select 'B', 2, 140
--静态的
select * from tbtest
select 姓名,max(case when 次数=1 then 结果 else ' 'end) as 次数1
,max(case when 次数=2 then 结果 else ' 'end) as 次数2
,max(case when 次数=3 then 结果 else ' 'end) as 次数3
,max(case when 次数=4 then 结果 else ' 'end) as 次数4
from tbtest
group by 姓名
--动态的
declare @s varchar(1000)
set @s='select 姓名'
select @s=@s+', max( case when 次数='''+rtrim(次数)+''' then 结果 else '''' end) as 次数'+rtrim(次数)
from (select distinct 次数 from tbtest) a
set @s=@s+' from tbtest group by 姓名'
exec(@s)
姓名 次数1 次数2 次数3 次数4
---------- ----------- ----------- ----------- -----------
A 90 120 110 100
B 100 140 0 0(2 行受影响)
下面的插入应该是静态的吧
insert tbTest
select 'A', 1, 90 union all
select 'A', 2, 120 union all
select 'A', 3, 110 union all
select 'A', 4, 100 union all
select 'B', 1, 100 union all
select 'B', 2, 140
select 'A', 1, 90 union all
select 'A', 2, 120 union all
select 'A', 3, 110 union all
select 'A', 4, 100 union all
select 'B', 1, 100 union all
select 'B', 2, 140这个不是限制了只有A、B两个人吗,在实际中会有A、B、C……,那插入tbTest时候就不能向上面这样写了吧
-------------------------------------------
完全可以,没有问题.楼主为什么不用实际数据测试一下呢,实践出真知,再结合print @sql打印出的语句体会一下.
这里的姓名只是分组,也就是对每个人都进行相同的处理,这种处理就是把次数值变成横向的列,如次数1,次数2...