又是第一次见到的SQL查询,复杂啊,头晕了。

sharplee82 2003-12-18 08:36:55
表A内容:
学生1 学生2 学生3 学生4 学生5
科目 上学期下学期 上学期下学期 上学期 下学期 上学期 下学期 上学期下学期
语文 80 90 30 69 87 65 60 60 87 65
数学 45 43 43 98 0 5 70 70 0 5

现要
转换成表B

学生1 学生2
科目 上学期 下学期 上学期 下学期
语文 80 90 30 69
数学 45 43 43 98
语文 87 65 60 60
数学 0 5 70 70
语文 87 65
数学 0 5
也就是说A表中确定每行显示几个学生的成积后,然后然后的相应的学生的成绩要接到
记录的尾部。
...全文
33 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 元老 2003-12-18
  • 打赏
  • 举报
回复
--例子:
/*--假设有数据

科目 学生1_上学期 学生1_下学期 学生2_上学期 学生2_下学期 学生3_上学期 学生3_下学期 学生4_上学期 学生4_下学期 学生5_上学期 学生5_下学期
------ ------------ ----------- ----------- ------------ ------------ ------------ ------------ ----------- ------------ -----------
语文 80 90 30 69 87 65 60 60 87 65
数学 45 43 43 98 0 5 70 70 0 5


现要需要一个存储过程,输入每页的学生数目,得到结果,假设输入2,得到结果:
页号 科目 学生1_上学期 学生1_下学期 学生2_上学期 学生2_下学期
----------- ---------- ----------- ----------- ----------- -----------
0 语文 80 90 30 69
0 数学 45 43 43 98
1 语文 87 65 60 60
1 数学 0 5 70 70
2 语文 87 65 NULL NULL
2 数学 0 5 NULL NULL

(所影响的行数为 6 行)

--*/
create table 表A(科目 varchar(10)
,学生1_上学期 int,学生1_下学期 int
,学生2_上学期 int,学生2_下学期 int
,学生3_上学期 int,学生3_下学期 int
,学生4_上学期 int,学生4_下学期 int
,学生5_上学期 int,学生5_下学期 int)
insert into 表A
select '语文',80,90,30,69,87,65,60,60,87,65
union all select '数学',45,43,43,98,0,5,70,70,0,5
go

--处理的存储过程
create proc p_qry
@pagesize int --每页要显示的学生记录数
as
declare @s1 varchar(8000),@s2 varchar(8000),@s3 varchar(8000)
,@i int,@j int
select @s1='',@s2='',@s3=''

select gid=cast((colid-2)/@pagesize/2 as varchar),name into #t
from syscolumns
where object_id('表A')=id and name<>'科目' order by colid
set @j=@pagesize-((@@rowcount/2) % @pagesize)

select @s1=case @i when gid then @s1 else @s1+',@'+gid+' varchar(8000)' end
,@s2=@s2+case @i when gid then '' else ' from 表A'',@'+gid+'=''select 页号='+gid+',科目' end+',['+name+']'
,@s3=case @i when gid then @s3 else @s3+'+'' union all ''+@'+gid end
,@i=gid
from #t

while @j>0
select @s2=@s2+',null,null',@j=@j-1

select @s1=substring(@s1,2,8000)
,@s2=substring(@s2,11,8000)+' from 表A'''
,@s3='exec('+substring(@s3,16,8000)+')'

exec('declare '+@s1+'
select '+@s2+'
'+@s3)
go

--调用
exec p_qry 2
go

--测试测试环境
drop table 表A
drop proc p_qry
zjcxc 元老 2003-12-18
  • 打赏
  • 举报
回复
/*--表结构不清晰.假设表结构为:
科目 学生1_上学期 学生1_下学期 ....
--*/

--处理的存储过程
create proc p_qry
@pagesize int --每页要显示的学生记录数
as
declare @s1 varchar(8000),@s2 varchar(8000),@s3 varchar(8000)
,@i int,@j int
select @s1='',@s2='',@s3=''

select gid=cast((colid-2)/@pagesize/2 as varchar),name into #t
from syscolumns
where object_id('表A')=id and name<>'科目' order by colid
set @j=@pagesize-((@@rowcount/2) % @pagesize)

select @s1=case @i when gid then @s1 else @s1+',@'+gid+' varchar(8000)' end
,@s2=@s2+case @i when gid then '' else ' from 表A'',@'+gid+'=''select 页号='+gid+',科目' end+',['+name+']'
,@s3=case @i when gid then @s3 else @s3+'+'' union all ''+@'+gid end
,@i=gid
from #t

while @j>0
select @s2=@s2+',null,null',@j=@j-1

select @s1=substring(@s1,2,8000)
,@s2=substring(@s2,11,8000)+' from 表A'''
,@s3='exec('+substring(@s3,16,8000)+')'

exec('declare '+@s1+'
select '+@s2+'
'+@s3)
go

--调用
exec p_qry 2
go
sharplee82 2003-12-18
  • 打赏
  • 举报
回复
是一个临时表.
victorycyz 2003-12-18
  • 打赏
  • 举报
回复
A表是不是经过查询得到的视图。

我觉得最好是从原表中查询得到B表。
sharplee82 2003-12-18
  • 打赏
  • 举报
回复
希望大侠们写过存储过程,因为学生的多少是动态变化的,并不是只有上面的五个。
txlicenhe 2003-12-18
  • 打赏
  • 举报
回复
SELECT 科目,学生1上学期,学生1下学期,学生2上学期,学生2下学期
FROM
(
select 科目,学生1上学期,学生1下学期,学生2上学期,学生2下学期,1 as flag from 表
union all
select 科目,学生3上学期,学生3下学期,学生4上学期,学生4下学期,2 as flag from 表
union all
select 科目,学生5上学期,学生5下学期,NULL,NULL,3 as flag from 表
) TMP
order by flag,科目 desc



sharplee82 2003-12-18
  • 打赏
  • 举报
回复
上面表A的内容显示的时候变了样,不过大家应该知道是什么样子的:-)
我看了这一样有点类似,不知谁能后在
http://expert.csdn.net/Expert/topic/2573/2573606.xml?temp=.5021173
邹建或马可的基础上直接生成我这里的表B。真的好感谢好感谢了。

34,591

社区成员

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

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