区分重复值组

时间:2014-05-23 11:24:34

标签: sql database oracle

假设我有下表table

+---------+---------+
| column1 | column2 |
+---------+---------+
| A       | B       |
| A       | A       |
| A       | A       |   
| C       | D       |
| B       | B       |
| B       | B       |
+---------+---------+

我正在查询数据库,以便获取table中的所有重复行:

SELECT *
  FROM table
 WHERE (column1,
        column2) IN (  SELECT column1,
                              column2
                         FROM table
                     GROUP BY column1,
                              column2
                       HAVING COUNT (*) > 1);

得到这个:

+---------+---------+
| column1 | column2 |
+---------+---------+
| A       | A       |
| A       | A       |   
| B       | B       |
| B       | B       |
+---------+---------+

有没有办法隔离每组重复的行,保存它们,然后获取下一组?最后,我会有不同的结果集:

Result set #1:
+---------+---------+
| column1 | column2 |
+---------+---------+
| A       | A       |
| A       | A       |   
+---------+---------+

Result set #2:
+---------+---------+
| column1 | column2 |
+---------+---------+
| B       | B       |
| B       | B       |   
+---------+---------+

我的想法是创建一个序列来枚举每个组,如下所示:

Result set #1:
+---------+----------+
| column1 | column2  |
+---------+----------+
| A       | A1       |
| A       | A2       |   
+---------+----------+

Result set #2:
+---------+----------+
| column1 | column2  |
+---------+----------+
| B       | B1       |
| B       | B2       |   
+---------+----------+

谢谢。

3 个答案:

答案 0 :(得分:1)

这样做你想要的吗?

select groupnum, column1, column2, seqnum
from (select t.*, count(*) over (partition by column1, column2) as cnt,
             dense_rank() over (order by column1, column2) as groupnum,
             row_number() over (partition by column1, column2 order by column1) as seqnum
      from table t
     ) t
where cnt > 1
order by groupnum;

答案 1 :(得分:0)

从逻辑角度来看......

A       | A
A       | A

......和......一样......

A       | A    | 2

为什么不呢:

SELECT column1, column2, COUNT(*)
FROM T
GROUP BY column1, column2
HAVING COUNT(*) > 1

你会得到一个结果,如......

A       | A    | 2
B       | B    | 2

...换句话说:每行代表一个整体。然后,如果需要,您可以轻松地“扩展”客户端代码中的每个组。

答案 2 :(得分:0)

试试这个:

SELECT column1,CASE WHEN RN=1 
                    THEN column2 
                    ELSE CONCAT(column2,convert(varchar(10), (RN-1))) 
               END as column2
FROM
 (SELECT column1,column2,ROW_NUMBER() OVER(PARTITION BY column1,column2 ORDER BY column2,column2) as RN
  FROM TableName) T

结果如下:

COLUMN1   COLUMN2
A         A
A         A1
A         B
B         B
B         B1
C         D

SQL Fiddle中的示例。