================================
第一个问题:
表名:欠费表 T_Fee(sno)学号 (Fee)欠费 04105124 2500.0
04105124 9100.0
04105125 2500.0
04105125 9100.0
04105126 2500.0
04105127 2500.0
04105127 9100.0
04105128 2500.0
04105128 9100.0 同一个学号有几个欠费记录,是一对多的关系要求查询结果是这样的(sno)学号 (Fee1)欠费1 (Fee2)欠费2 ……
04105124 2500.0 9100.0
04105125 2500.0 9100.0
04105126 2500.0 Null
04105127 2500.0 9100.0
04105128 2500.0 9100.0
这个欠费1,欠费2的顺序就根据数据表中存储的顺序来——顺着来的
===================================================================
第二个问题:表名:成绩表 T_Score(sno)学号 (KC)课程 (Score)成绩 04105124 会计 85
04105124 英语 80
04105124 微积分 6004105125 英语 50
04105125 会计 84
04105125 微积分 5404105126 微积分 40
04105126 英语 80
04105126 会计 60
同一个学号固定的有N条记录,是固定一对多的关系 比如 上面列举的3门课程
值得注意的是数据表中课程的顺序是被打乱的,也就是说不同学号对应的课程顺序都是不一样的
但是对应的内容都是固定的要求查询结果是这样的(sno)学号 (WJF)微积分 (YY)英语 (KJ)会计
04105124 60 80 85
04105125 54 50 84
04105126 40 80 60问题很急啊,加班在搞
我在这里谢谢各位大哥了
第一个问题:
表名:欠费表 T_Fee(sno)学号 (Fee)欠费 04105124 2500.0
04105124 9100.0
04105125 2500.0
04105125 9100.0
04105126 2500.0
04105127 2500.0
04105127 9100.0
04105128 2500.0
04105128 9100.0 同一个学号有几个欠费记录,是一对多的关系要求查询结果是这样的(sno)学号 (Fee1)欠费1 (Fee2)欠费2 ……
04105124 2500.0 9100.0
04105125 2500.0 9100.0
04105126 2500.0 Null
04105127 2500.0 9100.0
04105128 2500.0 9100.0
这个欠费1,欠费2的顺序就根据数据表中存储的顺序来——顺着来的
===================================================================
第二个问题:表名:成绩表 T_Score(sno)学号 (KC)课程 (Score)成绩 04105124 会计 85
04105124 英语 80
04105124 微积分 6004105125 英语 50
04105125 会计 84
04105125 微积分 5404105126 微积分 40
04105126 英语 80
04105126 会计 60
同一个学号固定的有N条记录,是固定一对多的关系 比如 上面列举的3门课程
值得注意的是数据表中课程的顺序是被打乱的,也就是说不同学号对应的课程顺序都是不一样的
但是对应的内容都是固定的要求查询结果是这样的(sno)学号 (WJF)微积分 (YY)英语 (KJ)会计
04105124 60 80 85
04105125 54 50 84
04105126 40 80 60问题很急啊,加班在搞
我在这里谢谢各位大哥了
select (sno)学号 ,max(Fee) as Fee1,min(Fee) as fee2 from table group by (sno)学号
select (sno)学号 ,min(Fee) as Fee1,max(Fee) as fee2 from table group by (sno)学号
set @sql=''
select @sql=@sql+',max(case when kc='''+kc+''' then Score else null end) as ['+kc+']'
from T_Score
group by kcexec (select sno'+@sql+' from T_Score group by sno')
into #t from T_Feedeclare @sql varchar(8000)
set @sql=''
select @sql=@sql+',max(case when (select count(*) from #t where sno=a.sno and id<=a.id)='+cast((select count(*) from #t where sno=a.sno and id<=a.id) as varchar)+' then fee else null end) as [fee'+cast((select count(*) from #t where sno=a.sno and id<=a.id) as varchar)+']'
from #t a
group by (select count(*) from #t where sno=a.sno and id<=a.id)exec (select sno'+@sql+' from #t a group by sno')
--所有语句没有测试
--建立测试
create table test(sno numeric(8),fee numeric(10,1))
insert into test select 04105124,2500.0
insert into test select 04105124,9100.0
insert into test select 04105125,2500.0
insert into test select 04105125,9100.0
insert into test select 04105125,9103.0
insert into test select 04105126,9100.0
--转入有自增列的表#
select *,IDENTITY(int)id
into # from test
--处理动态sql并执行
declare @sql varchar(8000),@max int,@temp int
set @sql=''
set @max=(select max(cnt) from(select count(*)cnt from # group by sno)a)--一个学号最后欠的学费记录数
set @temp=1
while(@temp<=@max)
begin
set @sql=@sql+',max(case when cnt_smaller='+cast(@temp as varchar)
+' then fee else null end) as [fee'+cast(@temp as varchar)+']'
set @temp=@temp+1
end
--select 'select sno'+@sql+' from # a group by sno'
exec ('select sno'+@sql+' from (select sno,fee,(select count(*) from # where sno=a.sno and id<=a.id)[cnt_smaller] from # a)a group by sno')
--结果
sno fee1 fee2 fee3
---------- ------------ ------------ ------------
4105124 2500.0 9100.0 NULL
4105125 2500.0 9100.0 9103.0
4105126 9100.0 NULL NULL
--删除测试
drop table test
drop table #
specialsoldier(雪地撒野~噢姐姐,我要回家)
好厉害啊
第一题测试通过:)
drop table #T_Score
drop table #temp
create table #table3(sno int,kc varchar(20),score int)
insert into #T_Score
select 04105124 , '会计' , 85 union
select 04105124 , '英语' , 80 union
select 04105124 , '微积分' , 60 unionselect 04105125 , '会计' , 50 union
select 04105125 , '英语' , 84 union
select 04105125 , '微积分' , 54 unionselect 04105126 , '会计' , 40 union
select 04105126 , '英语' , 80 union
select 04105126 , '微积分' , 60
--select * from table3
select sno '学号' , (select score from #T_Score where sno = outertable.sno and kc = '会计') as '会计',
(select score from #T_Score where sno = outertable.sno and kc = '英语') as '英语',
(select score from #T_Score where sno = outertable.sno and kc = '微积分') as '微积分'
into #temp
from #T_Score outertable select distinct(学号),会计,英语,微积分 from #temp