Subsribe to our RSS

latest reactions

Joshua Giese
wow, very simple. I was going to use a U …
up set
I think its a .... shame that Adobe ship …
vacuum cleaners
The dateformat thing often upsets me. Mo …
Phyllis
Hello Dav, Gary, and Tjarko, ... Thank y …
Tjarko
This one doesn't load either, aarghhh @G …

mxna feeds

understanding flex data management by using coldfusion livecycleCFConversations 6, Interview 2 - Michael Smith of Teratech - 07/05/08Jacksonville Code Camp Needs SponsorsTweetDeck, The Next Step In My Twitter AddictionWhy Can't I Run More Than One Server Instance at the Same Time?Learn about ColdFusion 8 in your backyard (free eseminars)How to decrypt Coldfusion datasource passwordsMore Thoughts On MVC, OOP, And Form Submissions In ColdFusionPoor Man's HTTP Compression with ColdFusionWormhole for Connecting Flex and ColdFusionIntro: HTML to Flex in a matter of daysGot cookies? (in your logs)4 ColdFusion Webinars In JulyTom Jordahl Rocks! - How to consume web services in ColdFusionColdFusion Event Gateway Reference

All files are strictly confidential: all information is classified.
© Copyright 2002 - 2008 mximize.com.
All right reserved.

MXNA webfeed

Visit Carlos GallupaPowered by ColdFusion MX 7.01

How to Remove, Delete double records but still have the values intact.

First we get all the double records. In this case I have a table called "test" with 2 columns "tst_id char(26) DEFAULT newid(),tst_name varchar(50)".

Secondly we use the GROUP attribute in the <cfquery> tag to only get SINGLE records. In this query we declare a temporary table (MSSQL - memory only) and we insert the SINGLE records in that table

then we DELETE all the double items and after that insert all the single items back into the original table from the temporary table.

Leaves you with all the double items removed!! BUT BE CAREFULL, this method doesn't take into account that records may have a relation with other tables in the database...

<cfquery>
SELECT  tst_id,tst_name
FROM test
WHERE UPPER(tst_name) IN (
  SELECT UPPER(tst_name) as tst_name
  FROM test
  GROUP BY tst_name
  HAVING (COUNT(UPPER(tst_name)) > 1)
)
ORDER BY tst_name
</cfquery>

<cfsavecontent variable="sqlStmt">
  DECLARE @tmp TABLE (tst_id char(36),tst_name varchar(50))

  <!--- Only insert single items of the double records --->
  <cfoutput query="qDouble" group="tst_name">
    INSERT INTO @tmp VALUES ('#tst_id#','#tst_name#')
  </cfoutput>

  <!--- Delete ALL double records --->
  DELETE
  FROM test
  WHERE UPPER(tst_name) IN (
    SELECT UPPER(tst_name) as tst_name
    FROM test
    GROUP BY tst_name
    HAVING (COUNT(UPPER(tst_name)) > 1)
  )
  
  <!--- Insert the single records back in the database --->
  INSERT INTO test (tst_id,tst_name)
  SELECT tst_id,tst_name FROM @tmp
</cfsavecontent>

<!--- Execute the query --->
<cfquery>
  #PreserveSingleQuotes(sqlStmt)#
</cfquery>

4135 viewed | 2 opinion(s)  | del.icio.us | Digg it | Tjarko @ 02/12/05 10:07 cet


Reactions:

jax wrote....

<cfquery>
DELETE FROM test WHERE EXISTS ( SELECTNameId FROMtest testDummy WHERE testDummy.tst_name= test.tst_nameAND testDummy.tst_Id <> test.tst_id )
</cfquery>

02 December 2005 10:22 cet  

Brian Kotek wrote....

You can also do this in one query (this is using Oracle's rowid, not sure if SQL Server has a similar concept):

DELETE FROM TEST
WHERE ROWID IN (
SELECT t.ROWID AS targetrowid
FROM TEST t,
(SELECTtst_id, tst_name, MAX (ROWID) AS maxrowid
FROM TEST
GROUP BY tst_id, tst_name
HAVING COUNT (*) > 1) m
WHERE t.ROWID != m.maxrowid AND t.tst_id = m.tst_id)

02 December 2005 16:11 cet  

Leave your comment

Your name


Your url/website/link/email....


Some room for your reaction is placed here..



A dozen apples are how many?? (12 would be a good answer for this)




URL en mail addresses are translated for you... life sometimes is that simple!!
profile-manuscript