SQL表的行列转换问题

kevenquwei 2007-11-29 03:48:43
请问用SQL语句,能实现以下功能吗?



请高手帮忙,先行感谢了!
...全文
113 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
anison 2007-11-30
  • 打赏
  • 举报
回复

create table tbStudentScore(
StudentNam varchar(10) ,
CourseNam varchar(40),
Score int,
primary key(StudentNam,CourseNam))

insert tbStudentScore select '张三','语文',78
union all select '李四','语文',82
union all select '王五','语文',74
union all select '李四','数学',98
union all select '王五','数学',65
union all select '张三','数学',31
union all select '李四','英语',56
union all select '张三','英语',88
union all select '张三','政治',73


select * from tbStudentScore

--删除测试环境
drop table tbStudentScore

declare @s varchar(8000)
select @s='select 姓名=isnull(StudentNam,''科目总分'')'
select @s=@s+','+quotename(CourseNam,'''')+'=sum(case CourseNam when '+quotename(CourseNam,'''')
+' then Score else 0 end) '
from tbStudentScore group by CourseNam
select @s=@s+',总分=sum(Score) from tbStudentScore group by StudentNam with rollup'
exec(@s)


JL99000 2007-11-30
  • 打赏
  • 举报
回复
今天是我第三天学sql server了
怎么还没学出来呢
declare @t table(code_i int,name_ch varchar(20),date_dt datetime,score_i int)
insert @t select 137,'贾悠屏','2007-11-17',892
union select 137,'贾悠屏','2007-11-18',1496
union select 137,'贾悠屏','2007-11-19',2208
union select 138,'张贵琴','2007-11-17',110
union select 138,'张贵琴','2007-11-18',111
union select 138,'张贵琴','2007-11-19',112
union select 139,'马言','2007-11-17',1100
union select 139,'马言','2007-11-18',1101
union select 139,'马言','2007-11-19',1102
--select * from @t

select code_i,name_ch,
max(case when date_dt = convert(datetime,min(date_dt)) then score_i else 0 end) as 日期1得分,
max(case when date_dt != convert(datetime,min(date_dt)) and date_dt != convert(datetime,max(date_dt)) then score_i else 0 end) as 日期2得分,
max(case when date_dt = convert(datetime,max(date_dt)) then score_i else 0 end) as 日期3得分
from @t
group by code_i,name_ch
错错错!郁闷啊
JL99000 2007-11-30
  • 打赏
  • 举报
回复
帮你顶下,也帮自己顶下
yhyhai 2007-11-30
  • 打赏
  • 举报
回复
我在用access时只需用transform...就行,而且不必知道有几列.怎么使用sql_server会那么麻烦啊!
jiangshun 2007-11-29
  • 打赏
  • 举报
回复
mark
conan304 2007-11-29
  • 打赏
  • 举报
回复
mark
-狙击手- 2007-11-29
  • 打赏
  • 举报
回复
也可以用动态SQL

普通行列转换

假设有张学生成绩表(t)如下

Name Subject Result
张三 语文  73
张三 数学  83
张三 物理  93
李四 语文  74
李四 数学  84
李四 物理  94

想变成
姓名 语文 数学 物理
张三 73  83  93
李四 74  84  94

create table #t
(
Name varchar(10) ,
Subject varchar(10) ,
Result int
)

insert into #t(Name , Subject , Result) values('张三','语文','73')
insert into #t(Name , Subject , Result) values('张三','数学','83')
insert into #t(Name , Subject , Result) values('张三','物理','93')
insert into #t(Name , Subject , Result) values('李四','语文','74')
insert into #t(Name , Subject , Result) values('李四','数学','83')
insert into #t(Name , Subject , Result) values('李四','物理','93')

declare @sql varchar(8000)
set @sql = 'select Name as ' + '姓名'
select @sql = @sql + ' , sum(case Subject when ''' + Subject + ''' then Result end) [' + Subject + ']'
from (select distinct Subject from #t) as a
set @sql = @sql + ' from #t group by name'
exec(@sql)

drop table #t

--结果
姓名 数学 物理 语文
---------- ----------- ----------- -----------
李四 83 93 74
张三 83 93 73

----------------------------------------------------
如果上述两表互相换一下:即

姓名 语文 数学 物理
张三 73  83  93
李四 74  84  94

想变成
Name Subject Result
张三 语文  73
张三 数学  83
张三 物理  93
李四 语文  74
李四 数学  84
李四 物理  94

create table #t
(
姓名 varchar(10) ,
语文 int ,
数学 int ,
物理 int
)

insert into #t(姓名 , 语文 , 数学 , 物理) values('张三',73,83,93)
insert into #t(姓名 , 语文 , 数学 , 物理) values('李四',74,84,94)

select 姓名 as Name,'语文' as Subject,语文 as Result from #t union
select 姓名 as Name,'数学' as Subject,数学 as Result from #t union
select 姓名 as Name,'物理' as Subject,物理 as Result from #t
order by 姓名 desc

drop table #t

--结果
Name Subject Result
---------- ------- -----------
张三 数学 83
张三 物理 93
张三 语文 73
李四 数学 84
李四 物理 94
李四 语文 74

(所影响的行数为 6 行)
fa_ge 2007-11-29
  • 打赏
  • 举报
回复

if exists(select * from sysobjects where name='t' and type='U')
drop table t
create table t
(
学号 int, 科目 varchar(20), 成绩 int
)

insert into t
select 11, '数学', 90 union all
select 11, '语文', 80 union all
select 11, '英语', 70 union all
select 22, '数学', 60 union all
select 22, '语文', 50 union all
select 22, '英语', 40

select * from t

select 学号,sum(case when 科目='数学' then 成绩 else 0 end) '数学',sum(case when 科目='语文' then 成绩 else 0 end ) '语文',sum(case when 科目='英语' then 成绩 else 0 end ) '英语'
from t group by 学号

fa_ge 2007-11-29
  • 打赏
  • 举报
回复
现在看到图了,可以实现,把数据贴出来
fa_ge 2007-11-29
  • 打赏
  • 举报
回复
看不到图

34,838

社区成员

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

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