------------------
姓名 次数 结果
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
如何实现啊?谢谢
...全文
22115打赏收藏
数据展示转化问题
库结构是:姓名、次数、结果 ------------------ 姓名 次数 结果 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 如何实现啊?谢谢
Create table test(姓名 char(1), 次数 int, 结果 int)
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, 140
declare @s varchar(1000)
select @s=coalesce(@s+',','')+' sum(case when 次数='+ltrim(次数) +' then 结果 end) [次数'+ltrim(次数)+']' from test group by 次数
if object_id('tbTest') is not null
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, 140
declare @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 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
在插入表数据时候,怎么变成动态的,
下面的插入应该是静态的吧
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
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, 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
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
create table #t(sname varchar(20)
,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 #t
declare @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 @sql