Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Duplicate Remover

Duplicate Remover

Scheduled Pinned Locked Moved Database
csharpasp-netdatabasecom
5 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vimalsoft Pty Ltd
    wrote on last edited by
    #1

    i have the Following Query

    select sa1 as[sa1],sa2 as [sa2],count(sa1) as [count] into #ttemp from EXP_REL_CLSH_CONT
    group by sa1,sa2
    having count(sa1)> 1
    order by sa1,sa2

    and my final query is this

    select c.sa1,c.sa2,c.dur1,c.dur2 from EXP_REL_CLSH_CONT C
    INNER JOIN #ttemp t
    on c.sa1 = t.sa1
    and c.sa2 = t.sa2

    as you can see there are Duplicates in the sa1 and sa2. So i want to delete any of record but one of each. so that means at last there should be 5 Records. that bring me these Duiplicate

    sa1 sa2 Dur1 der2

    6 7 3 2
    6 7 3 3
    354 867 1 2
    354 867 1 3
    354 872 1 2
    354 872 1 3
    356 867 1 2
    356 867 1 3
    356 872 1 2
    356 872 1 3

    Thanks

    Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.com vuyiswa@its.co.za http://www.itsabacus.co.za/itsabacus/

    N M 2 Replies Last reply
    0
    • V Vimalsoft Pty Ltd

      i have the Following Query

      select sa1 as[sa1],sa2 as [sa2],count(sa1) as [count] into #ttemp from EXP_REL_CLSH_CONT
      group by sa1,sa2
      having count(sa1)> 1
      order by sa1,sa2

      and my final query is this

      select c.sa1,c.sa2,c.dur1,c.dur2 from EXP_REL_CLSH_CONT C
      INNER JOIN #ttemp t
      on c.sa1 = t.sa1
      and c.sa2 = t.sa2

      as you can see there are Duplicates in the sa1 and sa2. So i want to delete any of record but one of each. so that means at last there should be 5 Records. that bring me these Duiplicate

      sa1 sa2 Dur1 der2

      6 7 3 2
      6 7 3 3
      354 867 1 2
      354 867 1 3
      354 872 1 2
      354 872 1 3
      356 867 1 2
      356 867 1 3
      356 872 1 2
      356 872 1 3

      Thanks

      Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.com vuyiswa@its.co.za http://www.itsabacus.co.za/itsabacus/

      N Offline
      N Offline
      Niladri_Biswas
      wrote on last edited by
      #2

      Just 1 small doubt.. For every record, the

      der2

      field is different. So how can u say that entire row is a duplicate(though the first 3 are same!)? Please clarify this.. and what is the output u are expecting please put it clearly :)

      Niladri Biswas

      V 1 Reply Last reply
      0
      • N Niladri_Biswas

        Just 1 small doubt.. For every record, the

        der2

        field is different. So how can u say that entire row is a duplicate(though the first 3 are same!)? Please clarify this.. and what is the output u are expecting please put it clearly :)

        Niladri Biswas

        V Offline
        V Offline
        Vimalsoft Pty Ltd
        wrote on last edited by
        #3

        i catch the Duplicate from two Records. i was able to do it like this

        select sa1,sa2,count(sa1) from EXP_REL_CLSH_CONT
        group by sa1,sa2
        having count(sa1)> 1
        order by sa1,sa2

        SELECT ACTV as ACTV_ID--, count([VENU]) as , sum([stud])
        into #temp
        FROM [dbo].SOL_ACTV_VENU
        group by ACTV
        having count(venu) > 1
        order by sum(stud)

        and i was deleting my Duplicates like this

        SET ROWCOUNT 1
        DELETE EXP_REL_CLSH_CONT
        from EXP_REL_CLSH_CONT C
        INNER JOIN #ttemp t
        on c.sa1 = t.sa1
        and c.sa2 = t.sa2
        SET ROWCOUNT 0

        but now in SQL Rowcount = 1 will stop everytime a duplicates is deleted so that it cannot delete the second record. Its Good. But how can i make it to loop and skip one record and delete one in this approach ?

        Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.com vuyiswa@its.co.za http://www.itsabacus.co.za/itsabacus/

        N 1 Reply Last reply
        0
        • V Vimalsoft Pty Ltd

          i catch the Duplicate from two Records. i was able to do it like this

          select sa1,sa2,count(sa1) from EXP_REL_CLSH_CONT
          group by sa1,sa2
          having count(sa1)> 1
          order by sa1,sa2

          SELECT ACTV as ACTV_ID--, count([VENU]) as , sum([stud])
          into #temp
          FROM [dbo].SOL_ACTV_VENU
          group by ACTV
          having count(venu) > 1
          order by sum(stud)

          and i was deleting my Duplicates like this

          SET ROWCOUNT 1
          DELETE EXP_REL_CLSH_CONT
          from EXP_REL_CLSH_CONT C
          INNER JOIN #ttemp t
          on c.sa1 = t.sa1
          and c.sa2 = t.sa2
          SET ROWCOUNT 0

          but now in SQL Rowcount = 1 will stop everytime a duplicates is deleted so that it cannot delete the second record. Its Good. But how can i make it to loop and skip one record and delete one in this approach ?

          Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.com vuyiswa@its.co.za http://www.itsabacus.co.za/itsabacus/

          N Offline
          N Offline
          Niladri_Biswas
          wrote on last edited by
          #4

          Try this

          declare @t table(sal1 int,sal2 int,dur1 int,dur2 int)
          insert into @t
          select 6,7,3,2 union all select 6,7,3,3 union all
          select 354,867,1,2 union all select 354,867,1,3 union all
          select 354,872,1,2 union all select 354,872,1,3 union all
          select 356,867,1,2 union all select 356,867,1,3 union all
          select 356,872,1,2 union all select 356,872,1,3

          select * from @t

          I am taking this RecordSet

          sal1 sal2 dur1 dur2
          6 7 3 2
          6 7 3 3
          354 867 1 2
          354 867 1 3
          354 872 1 2
          354 872 1 3
          356 867 1 2
          356 867 1 3
          356 872 1 2
          356 872 1 3

          Since you want any one of the record set so I can use either

          sal1 sal2 dur1 dur2
          6 7 3 2

          or

          sal1 sal2 dur1 dur2
          6 7 3 3

          If this assumption of mine is correct, the here is the answer

          select sal1,sal2,dur1,dur2 from(
          select row_number() over(partition by sal1,sal2 order by sal1,sal2) rn, sal1,sal2,dur1,dur2 from @t) X
          where rn = 1

          Output:

          sal1 sal2 dur1 dur2
          6 7 3 2
          354 867 1 2
          354 872 1 2
          356 867 1 2
          356 872 1 2

          Here I am considering only the first one for every duplicate entries What next you can do is put this record set in some temp table , delete the original one and then insert this record back into the table. Please let me know in case of any concern. Note - This code will work for Sql server 2005+ :)

          Niladri Biswas

          modified on Tuesday, November 24, 2009 9:31 AM

          1 Reply Last reply
          0
          • V Vimalsoft Pty Ltd

            i have the Following Query

            select sa1 as[sa1],sa2 as [sa2],count(sa1) as [count] into #ttemp from EXP_REL_CLSH_CONT
            group by sa1,sa2
            having count(sa1)> 1
            order by sa1,sa2

            and my final query is this

            select c.sa1,c.sa2,c.dur1,c.dur2 from EXP_REL_CLSH_CONT C
            INNER JOIN #ttemp t
            on c.sa1 = t.sa1
            and c.sa2 = t.sa2

            as you can see there are Duplicates in the sa1 and sa2. So i want to delete any of record but one of each. so that means at last there should be 5 Records. that bring me these Duiplicate

            sa1 sa2 Dur1 der2

            6 7 3 2
            6 7 3 3
            354 867 1 2
            354 867 1 3
            354 872 1 2
            354 872 1 3
            356 867 1 2
            356 867 1 3
            356 872 1 2
            356 872 1 3

            Thanks

            Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.com vuyiswa@its.co.za http://www.itsabacus.co.za/itsabacus/

            M Offline
            M Offline
            Mycroft Holmes
            wrote on last edited by
            #5

            User row_number partitioned over your key fields. This is a sample of a partition I use ROW_NUMBER() OVER( PARTITION BY ProductID, SubProductID, IssueLabel, Maturity, CurrencyID, Exposure Order by Exposure) as RowNo You need to include the ID field in the rest of the select and then delete any record where the RowNo > 1.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups