1

I have a Table such that looks like the following:

ID         TID        TNAME     CID     CNAME     SEQUENCE
-----------------------------------------------------------
200649     6125     Schalke    356954   Mirko         1
200749     6125     Schalke    356954   Mirko         1
200849     6125     Schalke    439386   Fred          1
200849     6125     Schalke    356954   Mirko         1
200849     6125     Schalke    495881   Michael       1
200949     6125     Schalke    401312   Felix         1
200949     6125     Schalke    495881   Michael       2

I would like to query this table so it only returns if ID and SEQUENCE are duplicated. i.e. it should only return:

200849     6125     Schalke    439386   Fred          1
200849     6125     Schalke    356954   Mirko         1
200849     6125     Schalke    495881   Michael       1

I have used having count(ID) > 1 but it will not return anything since CIDs are all unique.

Thanks for your help!

4

3 回答 3

3

我认为这是一种方法:

select a.*
from yourTable as a
inner join (
    select id, sequence 
    from yourTable 
    group by id, sequence 
    having count(id)>1) as b on a.id = b.id and a.sequence=b.sequence
于 2013-04-30T15:40:38.590 回答
2

像这样的东西?

SELECT b.id, 
       b.tid, 
       b.tname, 
       b.cid, 
       b.cname, 
       b.sequence 
FROM   (SELECT id, 
               sequence, 
               Count(*) CNT 
        FROM   table1 
        GROUP  BY id, 
                  sequence 
        HAVING Count(*) > 1) a 
       LEFT JOIN table1 b 
              ON b.id = a.id 
                 AND b.sequence = a.sequence 

结果

| 身份证 | 时间 | 名称 | 客户识别码 | 域名 | 序列 |
-------------------------------------------------- --------
| 200849 | 6125 | 沙尔克 | 439386 | 弗雷德 | 1 |
| 200849 | 6125 | 沙尔克 | 356954 | 米尔科 | 1 |
| 200849 | 6125 | 沙尔克 | 495881 | 迈克尔 | 1 |

查看演示

于 2013-04-30T15:38:16.273 回答
2

我喜欢对这些事情使用分析函数:

select t.*
from (select t.*, count(*) over (partition by id, sequence) as cnt
      from t
     ) t
where cnt > 1

这也为您提供了输出中每一行的重复数。

于 2013-04-30T15:44:22.200 回答