表A 结构如下
ID    NUM1  NUM2   NUM3   DATETIME
1       1     12    23     2011-12-15
2       1     12    34     2011-12-13
3       1     12    34     2011-12-18
4       1     13    45     2011-12-18
5       1     13    45     2011-12-15   想查出所有不重复的num2,及当时的id,num1,num3和最后出现的时间
最终返回如下结果:ID    NUM1  NUM2   NUM3   DATETIME
1       1     12    23     2011-12-15
3       1     12    34     2011-12-18
4      1     13    45     2011-12-18
 

解决方案 »

  1.   

    select distinct 表A.ID, 表A.NUM1, 表A.NUM2, 表A.NUM3, 表A.DATETIME from 表A,
    (selct NUM2,max(DATETIME) as DATETIME from 表A group by NUM2) viewA 
    where 表A.NUM2=viewA.NUM2 and 表A.DATETIME =viewA.DATETIME 
      

  2.   


    -- 看你的答案是不重复的NUM3吧?偶猜你NUM2是写错了
    SELECT * 
    FROM A 
    WHERE (NUM3,DATETIME) IN (SELECT NUM3,MAX(DATETIME) FROM A GROUP BY NUME);
      

  3.   


    SELECT *FROM A 
    WHERE ROWID!=(SELECT MAX(ROWID)FROM A B WHERE B.NUM2=A.NUM2 );
      

  4.   

    select ID, NUM1 ,NUM2 ,NUM3 ,DATETIME from (
    select ID, NUM1,NUM2,NUM3,DATETIME,
    row_number() over(partition by NUM3 order by DATETIME desc) rn 
    from A
    ) where rn=1
      

  5.   

    with a as ( 
    select 1 As id ,1 as num1 ,12 as num2,23 as num3,to_date('2011-12-15','yyyy-mm-dd') as datetime  from dual
    union all 
    select 2 As id ,1 as num1 ,12 as num2,34 as num3, to_date('2011-12-13','yyyy-mm-dd') as datetime  from dual 
    union all 
    select 3 As id ,1 as num1 ,12 as num2,34 as num3, to_date('2011-12-18','yyyy-mm-dd') as datetime  from dual
    union all 
    select 4 As id ,1 as num1 ,13 as num2,45 as num3, to_date('2011-12-18','yyyy-mm-dd') as datetime  from dual
    union all 
    select 5 As id ,1 as num1 ,13 as num2,45 as num3, to_date('2011-12-15','yyyy-mm-dd') as datetime  from dual

    select  id,num1,num2,num3,datetime from 
    ( select a.*,row_number() over( partition by num3 order by datetime desc  ) rn  from a ) where rn =1 
    你这个应该是 所有num3不重复的哟。