数据:  表t
                  
                  id          type      flag  
                  01          send      a  
                  01          send      b  
                  01          send      c  
                  02          send      a  
                  02          send      b  
                  01          rece      a  
                  01          rece      b  
  结果为:  
                  01          send    a  
                02          send    a  
id和flag是联合主键,查找id重复的记录:select id,type,flag  from (
                                 select id,type,flag,row_number()
                                 over(partition by id order by id) rm from t2) a where rm=1;     partition by id是什么意思, over(partition by id order by id) rm from t2) a 又是什么意思,请指教                   要删除id重复的数据:delete from t a 
                                   where rowid < 
                                   ( 
                                   select max(rowid) from t b 
                                   where a.id=b.id 
                                   )
                 
             这条sql语句又是什么意思,为什么要将t表看作两个表?
          学习中。

解决方案 »

  1.   

    row_number() over (partition by col1 order by col2)
    表示根据col1分组,在分组内部根据 col2排序
    而这个值就表示每组内部排序后的顺序编号(组内连续的唯一的)
      

  2.   

    这样的问题建议先baidu一下,查明函数的基本用法
      

  3.   

    数据:  表t 
                      
                      id          type      flag  
                      01          send      a  
                      01          send      b  
                      01          send      c  
                      02          send      a  
                      02          send      b  
                      01          rece      a  
                      01          rece      b  
      结果为:  
                      01          send    a  
                    02          send    a  
    delete from t t1,t2 where t1.id<>t2.id and t1.type==t2.type and t1.flag==t2.flag
      

  4.   

     要删除id重复的数据:
    delete from t a  where rowid < (  select max(rowid) from t b 
                                      where a.id=b.id ) 
    rowid:是oracle的一个伪列(oracle中的每个表都有,自动产生的),是在把数据添加到数据表中时,oracle就会自动为该行赋一个值(也就是该行数据有地址,且是唯一的);rowid < (  select max(rowid) from t b where a.id=b.id ) :查询出id值相同的最大的rowid值;然后再删除所有比rowid小的记录
      

  5.   

    可以再SQLPLUS中查看到rowid的值:select rowid,id,type,flag from 表t;