我有一个表和许多数据,如重复值和单个值。 但我想只得到重复的值数据,而不是单个值。
答案 0 :(得分:7)
SELECT columnWithDuplicates, count(*) FROM myTable
GROUP BY columnWithDuplicates HAVING (count(*) > 1);
答案 1 :(得分:0)
上述查询将显示重复的值。一旦你向业务用户提供,他们的下一个问题将是怎么回事?这些是怎么到达那里的?是否有重复的模式?通常更具信息性的是查看包含这些值的整行,以帮助确定为什么存在重复项。
-- this query finds all the values in T that
-- exist in the derived table D where D is the list of
-- all the values in columnWithDuplicates that occur more than once
SELECT DISTINCT
T.*
FROM
myTable T
INNER JOIN
(
-- this identifies the duplicated values
-- courtesy of Brian Roach
SELECT
columnWithDuplicates
, count(*) AS rowCount
FROM
myTable
GROUP BY
columnWithDuplicates
HAVING
(count(*) > 1)
) D
ON D.columnWithDuplicates = T.columnWithDuplicates