明天就考试了,有两道题想求答案,谢谢啦!一.现有航班表flights(flno, from, to, price),各属性含义依次为航班号、出发地、目的地、价格。
给定出发地和目的地,找出总价格最低的路线来。注意可以中转,比如从A出发到B,路线可以是从A到C,从C到D,再从D到B。
描述你的解决方案,(提供避免环路算法的,有奖励)二.用关系代数表示,已知选修关系SC(S#, C#,G),分别代表学生号、课程号、成绩
1.找出这样的学生他的每门课程的成绩都是最高的
2.找出偏科最严重的学生,也即其最高成绩与最低成绩之间的差最大。
3.找出平均成绩排在正中间的学生谢谢各位了!只要各位能提供个大概思路就行,再次感谢!

解决方案 »

  1.   

    第一个问题放到程序中还好说,在数据库中处理还有点难度啊。。
    首先构造一个结果集,根据出发地和目的地尽心分组,找到每个路段的价格最小值这里假设航班号是数字型,若不是可以添加一列自增长列
    select from, to, min(price),min(flno) from flights group by from, to然后基于此结果利用cte查找试试楼主
    第二个问题解答如下:
    1、
    with cte cteMaxScoreByCourse
    (
    select max(G)as maxCoureseScore from SC group by C#
    )
    ,
    cte ctePersonTotalScore(
    select S#,sum(G)as totalScore from SC group by S# )select S# from   ctePersonTotalScorecteMaxScoreByCourse where totalScore = select sum(maxCoureseScore ) from cteMaxScoreByCourse 
    思路是:首先找到每一个课程的最高分,然后找到每个学生的所有课程的总分,然后找所有课程总分等于每门课程最高分之和的学生即可。2、select *
    from(select S#,max(G)-min(G) as 'minusValue' from SC group by S#) as tempTable
    where minusValue=select max(minusValue) from tempTable
    思路是:首先找到每个学生的偏科分数,然后从这个结果集中找到偏科分数为最大的学生的所有信息。3、select S# from 
    (select S#,sum(G)as totalScore,rank() over (order by totalScore asc)as PaiMing from SC group by S#) as tempTable
    where paiming=略……
    成绩排在中间可以理解为排名排在中间,因此首先根据学生的学习成绩对他进行排名
    然后判断如果此班级学生数量为偶数,则返回中间的两个同学,若是奇数则返回一个同学的学号
    以上代码暂无环境,并未测试,这里给楼主抛砖引玉。
      

  2.   

    #1.第一题提供不了SQL,希望高手可以解决。
    #2.答案见下(仅供参考):
    select 'B', '语文', 78 union all
    select 'B', '数学', 99
    --他的每门课程的成绩都是最高的
    select b.学生号, a.课程号, a.最高成绩 from
    (select 课程号, 最高成绩=max(成绩) from SC group by 课程号) a
    inner join
    SC b
    on a.课程号 = b.课程号 and a.最高成绩 = b.成绩--找出第1名偏科最严重的学生
    select top 1 学生号,最好成绩=max(成绩), 最差成绩=min(成绩) from SC group by 学生号 order by (max(成绩) - min(成绩)) desc--找出平均成绩排在正中间的学生
    select * from
    (
    select number = row_number() over(order by 平均成绩 desc), * from
    (select 学生号, 平均成绩=AVG(成绩*1.0) from SC group by 学生号) t
    ) A
    where number > (select count(distinct 学生号)/2 from SC)
    and number <= (select count(distinct 学生号)/2+1 from SC)
      

  3.   

    下列不会环路,设最多五站(更多可增加类似的语句):
    create table tb(city1 nvarchar(20),city2 nvarchar(20),price int)
    insert into tb select 'Changi','Punggo',1900
    insert into tb select 'Changi','Bedok',1000
    insert into tb select 'Bedok','Kallang',1000
    insert into tb select 'Bedok','Qutram Park',1400
    insert into tb select 'Kallang','Park',700
    insert into tb select 'Qutram Park','Clementi',1000
    insert into tb select 'Kallang','Punggo',1700
    insert into tb select 'Kallang','Sembawang',2200
    insert into tb select 'Kallang','Woodland',2400
    insert into tb select 'Kallang','Bukit Batok',2500
    insert into tb select 'Kallang','Clementi',1700
    insert into tb select 'Punggo','Sembawang',1700
    insert into tb select 'Sembawang','Woodland',500
    insert into tb select 'Woodland','Bukit Batok',1400
    insert into tb select 'Bukit Batok','Clementi',500
    go
    declare @city1 nvarchar(20),@city2 nvarchar(20)
    set @city1='Changi'
    set @city2='Bukit Batok'
    ;with cte as(
    select @city1 as city1,city2,convert(nvarchar(20),'')as city3,convert(nvarchar(20),'')as city4,convert(nvarchar(20),'')as city5,price,(case when city2=@city2 then 1 else 0 end)as flg from tb where city1=@city1
    union all
    select @city1 as city1,city1 as city2,convert(nvarchar(20),'')as city3,convert(nvarchar(20),'')as city4,convert(nvarchar(20),'')as city5,price,(case when city1=@city2 then 1 else 0 end)as flg  from tb where city2=@city1
    union all
    select a.city1,a.city2,b.city2 as city3,convert(nvarchar(20),'')as city4,convert(nvarchar(20),'')as city5,a.price+b.price as price,(case when b.city2=@city2 then 1 else 0 end)as flg
     from cte a inner join tb b on a.city2=b.city1 where a.city3='' and b.city2!=a.city1 and a.flg=0
    union all
    select a.city1,a.city2,b.city1 as city3,convert(nvarchar(20),'')as city4,convert(nvarchar(20),'')as city5,a.price+b.price as price,(case when b.city1=@city2 then 1 else 0 end)as flg
     from cte a inner join tb b on a.city2=b.city2 where a.city3='' and b.city1!=a.city1 and a.flg=0
    union all
    select a.city1,a.city2,a.city3,b.city2 as city4,convert(nvarchar(20),'')as city5,a.price+b.price as price,(case when b.city2=@city2 then 1 else 0 end)as flg
     from cte a inner join tb b on a.city3=b.city1 where a.city4='' and b.city2!=a.city1 and b.city2!=a.city2 and a.flg=0
    union all
    select a.city1,a.city2,a.city3,b.city1 as city4,convert(nvarchar(20),'')as city5,a.price+b.price as price,(case when b.city1=@city2 then 1 else 0 end)as flg
     from cte a inner join tb b on a.city3=b.city2 where a.city4='' and b.city1!=a.city1 and b.city1!=a.city2 and a.flg=0
    union all
    select a.city1,a.city2,a.city3,a.city4,b.city2 as city5,a.price+b.price as price,(case when b.city2=@city2 then 1 else 0 end)as flg
     from cte a inner join tb b on a.city4=b.city1 where a.city5='' and b.city2!=a.city1 and b.city2!=a.city2 and b.city2!=a.city3 and a.flg=0
    union all
    select a.city1,a.city2,a.city3,a.city4,b.city1 as city5,a.price+b.price as price,(case when b.city1=@city2 then 1 else 0 end)as flg
     from cte a inner join tb b on a.city4=b.city2 where a.city5='' and b.city1!=a.city1 and b.city1!=a.city2 and b.city1!=a.city3 and a.flg=0
    )select city1,city2,city3,city4,city5,price from cte where flg=1 and price=(select min(price) from cte where flg=1 )
    go
    drop table tb
    /*
    city1                city2                city3                city4                city5                price
    -------------------- -------------------- -------------------- -------------------- -------------------- -----------
    Changi               Bedok                Qutram Park          Clementi             Bukit Batok          3900(1 行受影响)
    */