Subsribe to our RSS

latest reactions

jax
store the script path to the settings.cf …
Tjarko
hmmm not sure, it was a component that a …
Michael van Leest
Access="private" functions?? …
wese
It does work, as long as you know what y …
Allen
cfquery is not a scope …

Use OpenDNS

mxna feeds

First Bangalore ColdFusion UsergroupOCDev July Meeting: Geolocation with ColdFusion by Oğuz DemirkapıCF8 and FCKEditor Security threatColdFusion: Experiment Converting MS Word to HTML/PDF - Part 2 ExampleAnt Presentation FilesQuick example of Java via ColdFusion - Reading FLV MetadataCFUG NL sign up now!ColdFusion 8's OnMissingTemplate() - So Close To Being GoodWhy setting the right concurrent thread count is importantMy Encounter With a ColdFusion Detractor (Part 2)Why setting the right concurrent thread count is importantWhy setting the right concurrent thread count is importantColdFusion: Experiment Converting MS Word to HTML/PDF (At Last) - Part 2Examining my ColdFusion job searchAre there really ColdFusion jobs?

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

MXNA webfeed

Visit Carlos GallupaPowered by ColdFusion MX

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>

5840 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!!