Results 1 to 11 of 11
  1. #1
    Join Date
    Dec 2005
    Posts
    266

    Unanswered: DELETE items where Count(items) >1

    I cannot find an easy way to DELETE items which are > 1 time in my table (i am working with MS SQL 2000)

    Code:
      
    id	serial	isOk
    -------------------
    2	AAA	1
    3	BBB	0
    5	dfds	0
    6	CCC	1
    7	fdfd	 0
    8	AAA	0
    9	CCC	0
    I want to DELETE each Row IN

    Code:
     
    
    SELECT doublons.serial, Count(doublons.serial) AS  2Times
    FROM doublons
    GROUP BY doublons.serial
    HAVING  Count(doublons.serial)>1
    and WHERE isOK = 0

    in my exemple , after deleting, my table must look like

    Code:
     
    
    id	serial	isOk
    -------------------
    8	AAA	1
    9	CCC	1
    3	BBB	0
    5	dfds	0
    7	fdfd	0
    thank you for helping

  2. #2
    Join Date
    May 2005
    Location
    South Africa
    Posts
    1,365
    Provided Answers: 1
    delete doublons
    where id in (
    select a.id
    from doublons a, doublons b
    where a.id<b.id
    and a.serial=b.serial
    )

  3. #3
    Join Date
    Dec 2005
    Posts
    266
    your code seems not working well

    i have tried

    Code:
    DELETE  FROM Doublons
    WHERE doublons.serial < ANY
     (
    	SELECT DISTINCT doublons.serial
    	FROM doublons, doublons AS doublons_1
    	WHERE Exists
     	(
    		SELECT  doublons.serial
    		FROM  doublons AS doublons_2
                   WHERE doublons.Serial = doublons_2.Serial
    		AND  doublons.isOk=0
    	)
    )
    it works, but i dont know if it is the best method for large database with a few 1000 of rows

  4. #4
    Join Date
    Jun 2003
    Posts
    269
    Quote Originally Posted by pdreyer
    delete doublons
    where id in (
    select a.id
    from doublons a, doublons b
    where a.id<b.id
    and a.serial=b.serial
    )
    hmmmmmm.I dont think above solution will work for u.Check this,
    Code:
    delete d from  doublons d join
    (
    SELECT doublons.serial FROM doublons
    GROUP BY doublons.serial
    HAVING  Count(doublons.serial)>1
    ) as tm
    on tm.serial=d.serial
    where d.isOK=0
    I love cursor,though I never use it.Cos' I dont want to misuse it.
    ----------------------------------------------

    ----cheers
    ----http://mallier.blogspot.com

  5. #5
    Join Date
    Dec 2005
    Posts
    266
    mallier your code seems working fine

    thanks a lot

  6. #6
    Join Date
    May 2005
    Location
    South Africa
    Posts
    1,365
    Provided Answers: 1
    Sorry, hope this makes up?
    Code:
    delete a from doublons a
    where exists 
    (select 1
     from doublons b
     where a.serial=b.serial
     having count(*)>1 
    ) 
    and a.isok=0
    or
    Code:
    delete a 
    from doublons a, doublons b
    where a.id<>b.id
    and a.serial=b.serial
    and a.isok=0

  7. #7
    Join Date
    Sep 2003
    Location
    The extremely Royal borough of Kensington, London
    Posts
    778
    What you seem to fail to understand is that there are no duplicate rows, or 'items' as you like to call them, in your table.

    Perhaps in the future your 'company' should look at employing people that know how to design databases, rather than application 'developers' who wouldn't know a DB constraint if someone hit them over the head with one.
    Bessie Braddock: Winston, you are drunk!
    Churchill: And Madam, you are ugly. And tomorrow, I'll be sober, and you will still be ugly.

  8. #8
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    Got an 'axe' to 'grind'?
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  9. #9
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    yeah you need at least a 1000 posts before you can randomly flame someone.

    if there was'nt so much bad code and design out there, there would'nt be a such a need for good dba's. i have a endless supply of work due to bad database work generated by developers.

    btw, ireland for the irish.
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  10. #10
    Join Date
    Dec 2005
    Posts
    266
    what are uou speaking about bad code ? that is not my database and you have no idea how the database is updated

    thank you for the code , it works fine now

  11. #11
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    Thrassy was speaking in general terms.
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •