识别重复的行集

时间:2014-11-15 13:09:54

标签: oracle

我的表格如下所示。

   create table Testing(  
    inv_num varchar2(100),  
    po_num  varchar2(100),  
    line_num varchar2(100)  
    )  

以下数据。

Insert into Testing (INV_NUM,PO_num,line_num) values ('19782594','P0254836',1);  
Insert into Testing (INV_NUM,PO_num,line_num) values ('19782594','P0254836',1);  
Insert into Testing (INV_NUM,PO_num,line_num) values ('19968276','P0254836',1);  
Insert into Testing (INV_NUM,PO_num,line_num) values ('19968276','P0254836',1);  

我想要做的是使用相同的PO_num但不同的inv_num识别表中的多个项目。

我试过这个

  SELECT    
            T1.inv_num,  
            T1.Po_num,  
            T1.LINE_num  ,  
            count(*) over( partition by   
            T1.inv_num)myRecords  
        FROM testing T1  
         where  T1.Po_num  = 'P0254836'  
         group by   
            T1.inv_num,  
            T1.Po_num,  
            T1.LINE_num  
            order by t1.inv_num  

但这些不能给我预期的最终结果。

我想以下结尾。

INV_NUM PO_NUM      LINE_NUM     Myrecords
19782594    P0254836    1     1
19782594    P0254836    1     1
19968276    P0254836    1     2
19968276    P0254836    1     2

我哪里出错了?我真的很想确定那个po的INV_NUM的变化。

请注意,这是一个更大项目的一部分,我只选了一小部分来展示。

2 个答案:

答案 0 :(得分:1)

更新:

SELECT
  inv_num
  , po_num
  , line_num
  , DENSE_RANK() OVER (ORDER BY inv_num) "MyRecords"
FROM (
    SELECT
      po_num
      , inv_num
      , line_num
      , COUNT(line_num) OVER (PARTITION BY po_num, inv_num ORDER BY NULL) cnt
    FROM testing
)
WHERE cnt > 1;

返回

|  INV_NUM |   PO_NUM | LINE_NUM | MYRECORDS |
|----------|----------|----------|-----------|
| 19782594 | P0254836 |        1 |         1 |
| 19782594 | P0254836 |        1 |         1 |
| 19968276 | P0254836 |        1 |         2 |
| 19968276 | P0254836 |        1 |         2 |

SQL Fiddle

答案 1 :(得分:1)

也许这会有所帮助:

SELECT inv_num,
       po_num,
       line_num,
       DENSE_RANK() OVER (ORDER BY inv_num) AS rn
  FROM testing