2011-09-09 8 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 
関連する問題