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

SQLフィドル

于 2012-07-30T18:43:05.707 に答える