如何根据存在重复数据的列查找行

时间:2012-02-28 16:47:38

标签: sql-server-2008

假设我有一张这样的表 -

Code  | something
------------------
C01     abc
C02     mnt
C03     lkj
C01     dhl
C04     poi
C05     gtr
C02     rty
C01     asd
-------------------

现在,我只想要表中的那些行,在该列中哪些行多次Code。所以,输出就像 -

Code  | something
------------------
C01     abc
C01     dhl
C01     asd
C02     mnt
C02     rty
-------------------  

我是新的SQL服务器和查询的东西。拜托,帮帮我吧。 提前谢谢。

4 个答案:

答案 0 :(得分:1)

SELECT * FROM Table
WHERE code IN (
    SELECT Code FROM Table
    GROUP BY Code HAVING COUNT(*) > 1
)

答案 1 :(得分:0)

select *
from YourTable
where Code in
(
    select Code
    from YourTable
    group by Code
    having count(*) > 1
)

答案 2 :(得分:0)

;WITH codes AS 
(
  SELECT code 
  FROM dbo.table
  GROUP BY code 
  HAVING COUNT(*) > 1
)
SELECT code, something 
  FROM dbo.table AS t
  INNER JOIN codes AS c 
  ON t.code = c.code;

答案 3 :(得分:0)

SELECT T2.* 
FROM TableName, (SELECT Code from TableName T1 
                 GROUP BY T1.Code 
                 HAVING (COUNT(T1.Code) > 1)) T3
WHERE T2.Code = T3.Code