本帖最后由 suming000 于 2010-12-17 14:45:22 编辑

解决方案 »

  1.   


    select a.sno,a.scgrade as 课程1,b.scgrade as 课程2
    from
    (select * from tablename where cno = 1) a inner join
    (select * from tablename where cno = 2) b on a.sno = b.sno
    where a.scgrade > b.scgrade
      

  2.   

    SELECT * FROM [TB] where CNO in (1,2)
    order by SNO,CNO,SCGrade desc
      

  3.   

    --> 测试数据: #tb
    if object_id('tempdb.dbo.#tb') is not null drop table #tb
    go 
    create table #tb (SNO int,CNO int,SCGrade int)
    insert into #tb
    select 1,1,55 union all
    select 1,2,51 union all
    select 2,1,40 union all
    select 2,2,30 union all
    select 3,3,90--1、
    select sno,cj1=max(case when cno=1 then SCGrade end), 
    cj2=max(case when cno=2 then SCGrade end)  
    from #tb
    group by sno 
    having(max(case when cno=1 then SCGrade end)>max(case when cno=2 then SCGrade end))--2、
    select * from 
    (
    select sno,cj1=max(case when cno=1 then SCGrade end), 
    cj2=max(case when cno=2 then SCGrade end)  
    from #tb
    group by sno 
    )t
    where cj1>cj2--sno         cj1         cj2
    ------------- ----------- -----------
    --1           55          51
    --2           40          30
    --警告: 聚合或其他 SET 操作消除了空值。
    --
    --(2 行受影响)
      

  4.   

    /*if object_id('tb') is not null drop table tb
    create table tb(sno int,cno int ,score int)
    insert into tb
    select 1,1,55 union all
    select 1,2,51 union all
    select 2,1,40 union all
    select 2,2,30 union all
    select 3,3,90
    */
    select tb.* from (
    select case when score-(select score from tb where cno=2 and sno=t.sno)>0 then sno end as sno from tb t where cno=1)a
    inner join tb
    on a.sno=tb.sno/*1 1 55
    1 2 51
    2 1 40
    2 2 30*/
      

  5.   

    select a.SNO,a.SCGrade,b.SCGrade 
    from (select * from #tb  where CNO=1) a, (select * from #tb  where CNO=2) b where a.SNO=b.SNO and a.SCGrade>b.SCGrade
      

  6.   

    SELECT * FROM 
    (
    SELECT SNO,SUM(CASE CNO WHEN 1 THEN SCGRADE ELSE 0 END) as SCGRADE1
      ,SUM(CASE CNO WHEN 2 THEN SCGRADE ELSE 0 END) as SCGRADE2
    FROM T 
    GROUP BY SNO
    ) T
    WHERE T.SCGRADE1> T.SCGRADE2这个也行