一个表有域(occur datetime,domain int ,keyid int,content varchar2[200]),其中occur,domain,keyid是联合primary key,对于domain和keyid相同的数据,希望只返回最近的一条(4个域的内容都返回),这种sql语句如何写?急!!!!

解决方案 »

  1.   

    select occur,domain,keyid,content
    from (
    select occur,domain,keyid,content,
    row_number() over(partition by domain,keyid) rn
    from table)
    where rn=1
      

  2.   

    Quote]{select occur,domain,keyid,content 
    from ( 
    select occur,domain,keyid,content, 
    row_number() over(partition by domain,keyid) rn 
    from table) 
    where rn=1 
    }[/Quote] 
      

  3.   


    select m.* from tb m where not exists (select 1 from tb n where domain = m.domain and keyid = m.keyid and occur > m.occur)