0

I am trying to get multiple columns and show duplicate ItemCodes form a table. Ie. if an ItemCode is repeated more than once.

Using the query:

SELECT ItemCode    
    ,DuplicateCount = COUNT(*)
FROM IQR1
WHERE WhsCode = 01
GROUP BY
    ItemCode   
HAVING COUNT(*) > 1  
ORDER BY COUNT(*) DESC 

I get 38 records with duplicate counts of 2 each.

When I try to get another column, ItemName, I get only 37 records returned:

SELECT ItemCode
    ,ItemName    
    ,DuplicateCount = COUNT(*)
FROM IQR1
WHERE WhsCode = 01
GROUP BY
    ItemCode
   ,ItemName       
HAVING COUNT(*) > 1  
ORDER BY COUNT(*) DESC 

How do I correctly modify the first query to get multiple columns and still return 38 records?

4

1 に答える 1

1

最初のクエリを (派生テーブルに配置することにより) メイン テーブルに内部結合し、ItemCode で結合します。

SELECT
   I.*,
   D.DuplicateCount
FROM
   IQR1 I
   INNER JOIN (
      SELECT
         ItemCode,
         DuplicateCount = COUNT(*)
      FROM IQR1
      WHERE WhsCode = 01
      GROUP BY ItemCode   
      HAVING COUNT(*) > 1
   ) D ON I.ItemCode = D.ItemCode
ORDER BY DuplicateCount DESC
于 2013-01-10T09:30:52.053 に答える