SQL Server In a same table how to find Duplicate combination Column values are there or not?

mzmfm0qo  于 11个月前  发布在  其他
关注(0)|答案(3)|浏览(87)

I have one table called ServiceCodes in that havings Columns like Customer, companyId, ServiceCode, CompanySourc, NewlyInserted. I have to insert some records into this table. On that time I am inserting same customer and serviceCode values into that table. On that item I am updating this NewlyInserted bit into 1. By using this is there any way to get those Duplicated rows? And after that I want to remove those duplicated rows. In SSMS SQL example in ServiceCodes table

custID1,compid1,Service1233,CompSource1,0
custID2,compid1,Service1234,CompSource1,0
custID3,compid1,Service1235,CompSource1,0
custID4,compid1,Service1236,CompSource1,0
custID3,compid1,Service1235,CompSource1,1
custID4,compid1,Service1236,CompSource1,1

How to find these two rows servicecode and CustId combination are already there in that ServiceCodes table? I have tried Inner Join and Cross apply and all, it is not working for me.

EDIT:

UPDATE Servicecodes  
            SET isNeedstobeDelete = 1
            FROM Servicecodes
            CROSS APPLY (SELECT distinct Client,ServiceCode FROM Servicecodes WHERE NewlyInserted = 0) AS ApplicableClients
            WHERE
                NewlyInserted = 1
                AND [Servicecodes].Client= [ApplicableClients].Client
                AND NOT EXISTS (SELECT 1 FROM @tempservicecodes 
                WHERE [Servicecodes].servicecode = [ApplicableClients].servicecode 
                AND [Servicecodes].clientid = [ApplicableClients].clientid)

delete from servicecodes where isNeedstobeDelete = 1

it is taking so much time to run when data size is getting increased , so please give a solution which will take less amount of time ,please consider this aspect as well.

and there is no other conditions we have in this table. this deletion will occur every time user cicks one button.

mgdq6dx1

mgdq6dx11#

You can use a subquery to check for duplicates. SQL Server supports a delete t1 from table1 as t1 syntax where you can assign an alias to the table you want to delete from:

delete  sc1
from    ServiceCodes sc1
where   sc1.NewlyInserted = 1
        and exists
        (
        select  *
        from    ServiceCodes sc2
        where   sc2.NewlyInserted = 0
                and sc1.ServiceId = sc2.ServiceId
                and sc1.CustId = sc2.CustId
        )

Example at DBFiddle.

vof42yt1

vof42yt12#

A solution that uses the ROW_NUMBER() window function to search for duplicates.

with
  t as (
    select
      newlyinserted,
      row_number() over(partition by ServiceId, CustId
                        order by newlyinserted) as rn
    from ServiceCodes
  )
delete from t
where newlyinserted = 1 and rn > 1;

Try it on db<>fiddle .

Update:
If you need to "delete" and "update", then you need the MERGE statement (all about OUTPUT clause).

with
  t as (
    select
      *,
      row_number() over(partition by CustId, ServiceId
                        order by newlyinserted) as rn
    from ServiceCodes
  )
merge into t
using (values('')) as d(d)
  on newlyinserted = 1
when matched and rn = 1 then update
  set newlyinserted = 0
when matched and rn > 1 then delete
OUTPUT $action, deleted.*;

Try it on db<>fiddle .

xmq68pz9

xmq68pz93#

UPDATE @tempservicecodes
    SET isNeedstobeDelete = 1
    FROM (
        SELECT 
            MainTempServices.Client,MainTempServices.CompID,MainTempServices.ServiceCode   
        FROM @tempservicecodes MainTempServices  
        LEFT JOIN @tempservicecodes ReqTempServices 
            ON ReqTempServices.NewlyInserted = 0 
            AND MainTempServices.Client= ReqTempServices.Client
            AND MainTempServices.ServiceCode = ReqTempServices.ServiceCode  
        WHERE 
            MainTempServices.NewlyInserted = 1  
            AND ISNULL(ReqTempServices.ServiceCode, 0) = 0
    ) AS ApplicableServices  
    WHERE  
        NewlyInserted = 1  
        AND [@tempservicecodes].Client = ApplicableServices.Client  
        AND [@tempservicecodes].ServiceCode = ApplicableServices.ServiceCode   
        AND [@tempservicecodes].CompID = ApplicableServices.CompID

Delete from @tempservicecodes where isNeedstobeDelete = 1

Somehow, I Found this Left JOIN It is working Fine and Performance also Pretty Good.

相关问题