Delete Duplicates using CTE in SQL Server

This is a blog post for future reference and this has proved useful to me over the past year ever (ever since I found it online). And it's time to transfer the method from email to blog.

CTE is to me (from what little I know of advanced SQL concepts) a pretty powerful feature. It stands for Common Table Expression and is basically a temporary result set (table) of data that you can use within the same query execution.

I do this via a two step process, first query verifies that the CTE is generated properly, second one uses the CTE to delete the Duplicates.

Quite frankly, I still have no idea how exactly this works, I just know it has worked for me in the past. If any SQL Guru who comes upon this post wants to explain how this exactly works in the comments section it would be very appreciated.

view plain print about
1/* Build CTE and select records to verify everything looks good */
2WITH CTE AS
3(
4SELECT ID, RANK() OVER (PARTITION BY ID ORDER BY num) rnk
5FROM (SELECT *, ROW_NUMBER() OVER(ORDER BY name) num
6FROM ABC)
7X)
8SELECT * FROM CTE
9WHERE rnk >
1
10
11/* After verification, build CTE again and now Delete the duplicates */
12WITH CTE AS
13(
14SELECT ID, RANK() OVER (PARTITION BY ID ORDER BY num) rnk
15FROM (SELECT *, ROW_NUMBER() OVER(ORDER BY name) num
16FROM ABC)
17X)
18DELETE FROM CTE
19WHERE rnk > 1

The first query builds the CTE and we first verify that the built CTE is good, expected duplicates show up. The second query then deletes the duplicates.

In both queries, you have to replace the "ID" and "name" parts of the query based on your table. And you might now always know what exactly to use here, well then experiment. I usually start with using the duplicate column in the "name" part.

This has been of use particularly in cases where the tables have duplicate data but there is no PK (or a good PK) available to drive the deletion. And maybe there is an easier way too now.

ColdFusion 9's ORM Entity Load Functions

Update: Demo is currently unavailable.

This is a blog post, mainly so I have a good single place to refer the many varities of EntityLoad() functions available in ColdFusion 9. EntityLoad() is the main method you will be using to retreive objects for your persistent CFCs.

Demo currently unavailable.

view plain print about
1<cfscript>
2    ormreload();
3    import Logic.*;        
4
</cfscript>
5
6<div id="ex1" class="hidden">        
7    <cfset arrAllUsers = EntityLoad("Users")>
8    
9</div>
10
11<div id="ex2" class="hidden">
12    
13    <cfset arrFiveUsers = EntityLoad("Users",{},{maxResults=5})>
14        
15    <cfdump var="#arrFiveUsers#">
16</div>
17
18<div id="ex3" class="hidden">
19    
20    <cfset arrUniqueUser = EntityLoad("Users",228)>
21        
22    <cfdump var="#arrUniqueUser#">
23</div>
24
25<div id="ex4" class="hidden">
26    <cfset objUniqueUser = EntityLoad("Users",228,true)>
27            
28    <cfdump var="#objUniqueUser#">
29</div>
30
31<div id="ex5" class="hidden">
32    
33    <cfset objFilteredUser = EntityLoad("Users",{FirstName="User FN 1", LastName="User LN 1"},true)>
34    
35    <cfdump var="#objFilteredUser#">
36</div>
37
38<div id="ex6" class="hidden">
39    
40    <cfset arrFilteredUser = EntityLoad("Users",{FirstName="User FN 4000"})>
41        
42    <cfdump var="#arrFilteredUser#">
43    
44</div>
45
46<div id="ex7" class="hidden">
47            
48    <cfset arrSortedUser = EntityLoad("Users",{},"FirstName asc",{maxResults=5})>
49                
50    <cfdump var="#arrSortedUser#">
51</div>
52
53<div id="ex8" class="hidden">
54    <cfset arrSortedUser2 = EntityLoad("Users",{},"LastName asc, FirstName desc",{maxResults=2})>
55        
56    <cfdump var="#arrSortedUser2#">
57</div>

And there you have it folks.

ColdFusion 9 cfgrid with ORM: Part 1

One of the exciting new feature in ColdFusion 9 is its ORM implementation. ORM in simplest terms allows us to access the database using objects, or in in even more simpler terms you don't have to write any queries for simple CRUD functions. To learn more about ColdFusion 9 ORM visit Adobe's ColdFusion 9 ORM Docs and Introducing ORM in ColdFusion 9 Beta.

This post will look at implementing a cfgrid using ORM. This will be first in a series starting from just implementing the grid to read data ending with a full fledged CRUD cfgrid with ORM.

[More]

Delete and Drop All Tables from SQL Server Database

This post is more for record keeping. Found this useful little SQL on another site and posting it here will make it accessible to me forever.

[More]

/* ChartBeat Testing */