tbM:id,kscs,csdc//id号,cscs考试次数,csdc考试等次
数据:
     id       kscs    csdc     
   1001001     1      不合格
   1001001     2      合格
   1001002     1      合格
   1001003     1      不合格
   1001003     2      不合格
   1001003     3      合格
假定total为kscs的计数(即count(kscs))。
查询:total为1但kscs的最大值大于1的所有行(用于检测数据的合理性)
    

解决方案 »

  1.   


    --我怎么感觉怪怪的..total要为1,又说最大值,那就只有一个kscs求最大????
    SQL> with tbM as
      2  (
      3  select '1001001' id , 1 kscs, '不合格'csdc from dual union all
      4  select '1001001' id , 2, '合格'csdc from dual union all
      5  select '1001002' id , 1, '合格'csdc from dual union all
      6  select '1001003' id , 1, '不合格'csdc from dual union all
      7  select '1001003' id , 2, '不合格'csdc from dual union all
      8  select '1001003' id , 3, '合格'csdc from dual
      9  )
     10  select * from tbM a where exists(
     11  select 1 cnt from tbM b where a.id = b.id group by id having  count(b.kscs)=1
     12  )
     13  and a.kscs > =1
     14  /ID            KSCS CSDC
    ------- ---------- ------
    1001002          1 合格SQL> 
      

  2.   

    select 
    from tbM t
    where kscs>1 and not exists(
      select 1 from tbM
      where id=t.id and kscs<t.kscs)
      

  3.   

    select后的*忘记写了,自己补下吧...
      

  4.   

    这么写应该更符合楼主的意思
    select * from(
      select id,kscs,csdc,count(1)over(partition by id)total from tbM)
    where kscs>1 and total=1
      

  5.   

    自己写了一个,结果是正确的,但不能显示kscs、ksdc:
    SELECT id  from TbM 
    group by id
    having Max(kscs)>1 and count(kscs)=1
      

  6.   

    wildwave:不知partition是何意思?为何用在此处?
      

  7.   

    分析函数中的一个参数
    在这里:total为:按id分组,当前id所在分组的count
      

  8.   

    我用的是小型数据库sql anywhere 8.0.1,没有over函数。
    不知用sql怎样模拟over?
      

  9.   

    select * from (select id,count(kscs) as total1,max(csdc) as csdc,max(kscs) as kscs
    from tbM
    group by id
    having total1=1) a
    where kscs>1
      

  10.   

    不是oracle数据库啊
    2楼的and a.kscs > =1
    等号去掉应该就可以了