数据展示转化问题

am2002cnfj 2007-06-21 11:57:37
库结构是:姓名、次数、结果

------------------
姓名 次数 结果
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

如何实现啊?谢谢
...全文
221 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
lwl0606 2007-06-21
  • 打赏
  • 举报
回复
名字没有限制啊,group by 姓名,有多少不同的姓名都会显示出来
am2002cnfj 2007-06-21
  • 打赏
  • 举报
回复
上面两种方式都把姓名限死了,如果姓名是动态的,怎么处理啊?
chuifengde 2007-06-21
  • 打赏
  • 举报
回复
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 次数

exec('select 姓名,'+@s+' from test group by 姓名')
hellowork 2007-06-21
  • 打赏
  • 举报
回复
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
*/

am2002cnfj 2007-06-21
  • 打赏
  • 举报
回复
搞定了,感谢楼上的各位兄弟
hellowork 2007-06-21
  • 打赏
  • 举报
回复
这个不是限制了只有A、B两个人吗,在实际中会有A、B、C……,那插入tbTest时候就不能向上面这样写了吧
-------------------------------------------
完全可以,没有问题.楼主为什么不用实际数据测试一下呢,实践出真知,再结合print @sql打印出的语句体会一下.
这里的姓名只是分组,也就是对每个人都进行相同的处理,这种处理就是把次数值变成横向的列,如次数1,次数2...
ojuju10 2007-06-21
  • 打赏
  • 举报
回复
这个没有问题的!
am2002cnfj 2007-06-21
  • 打赏
  • 举报
回复
在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

这个不是限制了只有A、B两个人吗,在实际中会有A、B、C……,那插入tbTest时候就不能向上面这样写了吧
hellowork 2007-06-21
  • 打赏
  • 举报
回复
楼主指的动态是指要交叉处理的数据是另一个查询的结果吗?如果不是的话,那这些动态数据是怎么来的?请楼主把相关情况描述得清楚和全面些.
am2002cnfj 2007-06-21
  • 打赏
  • 举报
回复
这个数据是动态,在生成临时表时候要怎么动态的
hellowork 2007-06-21
  • 打赏
  • 举报
回复
上面的插入语句是在创建测试用的数据,是静态的,楼主想要表明什么?
am2002cnfj 2007-06-21
  • 打赏
  • 举报
回复
在插入表数据时候,怎么变成动态的,
下面的插入应该是静态的吧
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
ojuju10 2007-06-21
  • 打赏
  • 举报
回复
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

(2 行受影响)
yrwx001 2007-06-21
  • 打赏
  • 举报
回复

(影響 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
yrwx001 2007-06-21
  • 打赏
  • 举报
回复
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

exec(@sql)

drop table #t

34,837

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧