表:
create table score 
(
sid int identity primary key,
sno varchar(3) not null, 
    cno varchar(5) not null, 
    degree numeric(10, 1) not null

go用sql查询:
查询score中选学一门以上课程的同学中分数并非为本门课程最高分成绩的记录。

解决方案 »

  1.   

    create table score  
    (
    sid int identity primary key,
    sno varchar(3) not null,  
      cno varchar(5) not null,  
      degree numeric(10, 1) not null
    )  
    go
      

  2.   

    select * from score where degree not in (select max(degree) from score group by sno having count(cno) > 1)
      

  3.   

    错了,应该是,这么写好像麻烦,等高手
    select * from score where degree not in (select max(degree) from score group by sno having count(cno) > 1) and sno in (select sno from score group by sno having count(cno) >1)
      

  4.   

    select t.* from
    (select *,id=row_number() over(partition by cno order by degree desc) from score) t
    ,(select sno from score group by sno having count(cno)>=1)  t1
     where  t.sno=t1.sno and id>1
      

  5.   

    SELECT *
    FROM score s
    WHERE 1=
    (SELECT COUNT(*)
    FROM score sc
    WHERE s.cno=sc.cno AND sc.degree>=s.degree
    GROUP BY sc.cno)
    AND s.sno IN
    (SELECT sno
    FROM score
    GROUP BY sno
    HAVING count(*)>1)
      

  6.   

    学习5楼的查询,个人认为having count(cno)>=1需要修改如下select t.* from
    (select *,id=row_number() over(partition by cno order by degree desc) from score) t
    ,(select sno from score group by sno having count(sno)>1)  t1
     where  t.sno=t1.sno and id>1
      

  7.   

    楼主要求“非为本门课程最高分成绩的记录”,修正一下7楼代码
    SELECT *
    FROM score s
    WHERE 1<
    (SELECT COUNT(*)
    FROM score sc
    WHERE s.cno=sc.cno AND sc.degree>=s.degree
    GROUP BY sc.cno)
    AND s.sno IN
    (SELECT sno
    FROM score
    GROUP BY sno
    HAVING count(*)>1)
      

  8.   


    create table score1  
    (
    sid int identity primary key,
    sno varchar(3) not null,  
      cno varchar(5) not null,  
      degree numeric(10, 1) not null
    )  
    go
    insert into score1 (sno,cno,degree)
    select 001,001,80 union all
    select 001,002,90 union all
    select 001,003,100 union all
    select 002,001,60 union all
    select 002,002,95 union all
    select 003,001,90 union all
    select 004,004,70 union all
    select 004,002,60select score1.* from score1
    inner join (select cno,max(degree) as degree from score1 group by cno) aa
    on aa.cno = score1.cno and score1.degree <> aa.degree
    where sno in 
    (select sno from score1 group by sno having count(cno) > 1)sid    sno     cno     degree
    1 1 1 80.0
    4 2 1 60.0
    8 4 2 60.0
    2 1 2 90.0
      

  9.   

    SELECT *
    FROM score1 s
    WHERE 1<
        (SELECT COUNT(*)
        FROM score1 sc
        WHERE s.cno=sc.cno AND sc.degree>=s.degree
        GROUP BY sc.cno)
      

  10.   

    code:where 1<
    这1代表什么意识
      

  11.   

    select m.* from score m ,
    (select cno , max(degree) degree from score group by cno) n
    where m.cno = n.cno and m.degree < n.degree and m.sno in
    (select sno from score group by sno having count(1) > 1)select m.* from score m ,
    (select cno , max(degree) degree from score group by cno) n,
    (select sno from score group by sno having count(1) > 1) t
    where m.cno = n.cno and m.degree < n.degree and m.sno = t.sno