1
我有一個表和許多數據,如重複值和單個值。 但我只想獲取重複值數據,而不是單個值。如何從一個表中獲取重複數據?
我有一個表和許多數據,如重複值和單個值。 但我只想獲取重複值數據,而不是單個值。如何從一個表中獲取重複數據?
SELECT columnWithDuplicates, count(*) FROM myTable
GROUP BY columnWithDuplicates HAVING (count(*) > 1);
上述查詢將顯示重複的值。一旦你提供給商業用戶,他們的下一個問題將會發生什麼?這些是如何到達那裏的?有重複的模式嗎?更常見的是查看包含這些值的整行以幫助確定爲什麼有重複。
-- 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