2011-09-09 49 views

回答

7
SELECT columnWithDuplicates, count(*) FROM myTable 
GROUP BY columnWithDuplicates HAVING (count(*) > 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 
相關問題