1

我有代码可以抓取 SQL 表中的重复项并按跟踪号对它们进行分组。我想看到重复的每一行,而不仅仅是让它们分组。获取重复组的代码如下:

Select
      CarrierID
    , Mode
    , TrackingNumber
    , PickupID
    , Reference1
    , Reference2
    , Quantity
    , BilledWeight
    , ActualWeight
    , Zone
    , ServiceLevel
    , PickupDate
    , SenderCompany
    , SenderAddress
    , SenderCity
    , SenderState
    , SenderZip
    , ReceiverCompany
    , ReceiverAddress
    , ReceiverCity
    , ReceiverState
    , ReceiverZip
    , FreightCharge
    , Fuel
    , Accessories
    , TotalCharges
    , WrongName
    , WrongCompany
    , WrongAddress
    , WrongCity
    , WrongState
    , WrongZip
    , WrongCountry
    , CorrectedName
    , CorrectedCompany
    , CorrectedAddress
    , CorrectedCity
    , CorrectedState
    , CorrectedZip
    , CorrectedCountry
    , Count(TrackingNumber) as TrackingNumberTotal
    , Count(TotalCharges) as NumberofDuplicates
from Prasco_GencoShipments
group by 
      TrackingNumber
    , TotalCharges
    , CarrierID
    , Mode
    , TrackingNumber
    , PickupID
    , Reference1
    , Reference2
    , Quantity
    , BilledWeight
    , ActualWeight
    , Zone
    , ServiceLevel
    , PickupDate
    , SenderCompany
    , SenderAddress
    , SenderCity
    , SenderState
    , SenderZip
    , ReceiverCompany
    , ReceiverAddress
    , ReceiverCity
    , ReceiverState
    , ReceiverZip
    , FreightCharge
    , Fuel
    , Accessories
    , TotalCharges
    , WrongName
    , WrongCompany
    , WrongAddress
    , WrongCity
    , WrongState
    , WrongZip
    , WrongCountry
    , CorrectedName
    , CorrectedCompany
    , CorrectedAddress
    , CorrectedCity
    , CorrectedState
    , CorrectedZip
    , CorrectedCountry
having (count(TrackingNumber) > 1 and (count(TotalCharges) > 1))
4

2 回答 2

2

如果 CTE 可用(也可以通过子选择完成):

WITH dups AS (
        SELECT TrackingNumber, TotalCharges
        FROM Prasco_GencoShipments
        GROUP BY TrackingNumber, TotalCharges
        HAVING COUNT(*) > 1
        )
SELECT ta.*
FROM Prasco_GencoShipments ta
JOIN dups du ON du.TrackingNumber = ta.TrackingNumber AND du.TotalCharges = ta.TotalCharges
ORDER BY
      TrackingNumber
      , TotalCharges
        ;
于 2012-07-30T19:07:22.897 回答
0

查找field1的重复项(和field2,已评论):

SELECT t1.*

    FROM test t1

    INNER JOIN test t2
    ON t2.field1 = t1.field1 -- AND t2.field2 = t1.field2

    WHERE t1.id <> t2.id

SQLFiddle

于 2012-07-30T18:43:05.707 回答