数据表如下学号    考试次数     是否通过
001       1             0
001       2             0
001       3             1
002       1             1
003       1             0
003       1             0要求查询结果:
学号         是否通过
001            1
002            1
003            0

解决方案 »

  1.   

    select 学号  , max(是否通过) 是否通过 from tb group by 学号
      

  2.   

    SELECT
        学号,
        SUM(是否通过) AS 是否通过
    FROM tb
    GROUP BY 学号
      

  3.   

    create table tb(学号  varchar(10) ,  考试次数 int,   是否通过 int)
    insert into tb values('001' ,     1     ,       0 )
    insert into tb values('001' ,     2     ,       0 )
    insert into tb values('001' ,     3     ,       1 )
    insert into tb values('002' ,     1     ,       1 )
    insert into tb values('003'  ,   1      ,      0 )
    insert into tb values('003'  ,   1      ,      0 )
    goselect 学号  , max(是否通过) 是否通过 from tb group by 学号drop table tb/*
    学号         是否通过        
    ---------- ----------- 
    001        1
    002        1
    003        0(所影响的行数为 3 行)
    */
      

  4.   

    /*表的内容是  
    学号    考试次数    是否通过 
    001      1            0 
    001      2            0 
    001      3            1 
    002      1            1 
    003      1            0 
    003      1            0 要求查询结果: 
    学号        是否通过 
    001            1 
    002            1 
    003            0 
    */CREATE table #t  
    (code varchar(8) not null,
     times int not null,
     flag  smallint not null,
    )
    insert into #t
    select '001',1,0
    UNION ALL
    select '001',2,0
    UNION ALL
    select '001',3,1
    UNION ALL
    select '002',1,1
    UNION ALL
    select '003',1,0
    UNION ALL
    select '003',2,0select code,case when sum(flag)>0 then 1 else 0 end as flag from #t
    group by codeDROP TABLE #T/*
    (6 行受影响)
    code     flag
    -------- -----------
    001      1
    002      1
    003      0(3 行受影响)*/
      

  5.   

    select 学号,max(是否通过)
    from 表名
    group by 学号;
      

  6.   

    谢谢
    “是否通过”是bit类型有点麻烦
      

  7.   

    select 学号,sum(是否通过)是否通过
    from table 
    group by 学号   
      

  8.   

    select 学号,sum(是否通过)是否通过 
    from table 
    group by 学号  
    正解
      

  9.   

    create table #tb11(学号 varchar(10) ,考试次数 int,是否通过 bit)
    insert into #tb11 values('001',1,0)
    insert into #tb11 values('001',2,0)
    insert into #tb11 values('001',3,1)
    insert into #tb11 values('002',1,1)
    insert into #tb11 values('003',1,0)
    insert into #tb11 values('003',1,0)
    select * from #tb11select 学号,是否通过 from #tb11 where 是否通过=1
    union all
    select top 1 学号,是否通过 from #tb11 b where not exists (select * from #tb11 where 学号=b.学号 and 考试次数>b.考试次数) and 是否通过=0学号         是否通过
    ---------- -----
    001        1
    002        1
    003        0(3 行受影响)
      

  10.   

    select 学号  , max(是否通过) 是否通过 from tb group by 学号
      

  11.   

    1.改变“是否通过”的字段类型为int,用以下简短语句:
    select 学号  , max(是否通过) 是否通过 from tb group by 学号
    2.用9楼的方法,使用复杂的查询语句。
      

  12.   

    select 学号   
    max(是否通过) 是否通过 
    from table 
    group by 学号
      

  13.   

    select 学号,max(是否通过) 
    from 表名 
    group by 学号;
      

  14.   

    select 学号,max(是否通过) 
    from 表名 
    group by 学号;
    正确
      

  15.   

    select 学号,SUM(是否通过)
    from 数据表
    group by 学号