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.

Browser Detection using jQuery

After finally getting this blog back up online, the next step was to upgrade to the latest blogCFC version (which ships with the awesome theme you see). After being done with that, I noticed this is the first time I had used the blog with Chrome, and the one thing that didn't work in Chrome is display of the RSS feed. And no better way to remind myself how to accomplish this again in the future than blogging it!

The RSS in IE/Firefox is styled (I believe because of the in built RSS reader?):

The RSS in Chrome is not styled:

One good solution to this would be to just use the Feedburner link for the RSS feed (http://feeds.feedburner.com/ColdFusionRIA) and it displays good in all browsers. But I saw it as a good opportunity to use jQuery to redirect the click based on the browser. So, now if you click the RSS link on the navigation, it will show the default blogCFC RSS in IE/Firefox but for Chrome it will go to the feedburner link.

Now the main issue I encountered (and the solution is documented at various places on the internet) is that the built in jQuery browser detection doesn't differentiate between chrome and safari. You can see this at work here (try it with different browsers)

On Chrome, the default output will detect safari. So some changes had to be made to that.

Really simple to do using jQuery too. Let's look at the code.

[More]

cffileupload Tag - Uploading Multiple Files

Back in beta, I had made a post on this new tag in ColdFusion 9. It creates a Flash interface allowing the user to upload multiple files to the server. A pretty easy way to achieve something powerful. Back in beta, there was also a bug with the funtionality. It was that ColdFusion was not automatically passing back Success, Failure messages. With the release now, that has been taken care of, so we can revise the beta code.

Demo currently unavailable

Lets take a look at how simple the code is

[More]

cffileupload Tag - Event Handlers

Continuing with exploring the new multi-file upload tag functionality in ColdFusion 9, this post will build on the previous post and add event handlers. We are able to work with 3 event handlers:

  1. onComplete - Called after each file finishes uploading
  2. onError - Called when a file upload errors out
  3. onUploadComplete - Called when all the files finish uploading
Demo currently unavailable.

Lets take a look at the code.

[More]

Installing ColdFusion 9 64-but on Windows 7 64-bit Experience

This weekend I got me a new laptop from Dell (Studio 15). I got it with 4 GB RAM (to me it has become a minumum now) which naturally meant going with a 64-bit OS. So, I now had this nice new laptop with Windows 7 x64 and once I was done cleaning out the bloatware, it was time to install ColdFusion. It turned out to be quite an experience and took much longer then I anticipated. But first, lets get one thing straight here, Adobe doesn't really provide any documentation on what you need to do to install CF properly on IIS 7.5. There is no, "verify these windows component are installed" document or screen. In any case, I had installed ColdFusion 9 on Windows 32-bit succesfully, and needless to say I felt (over)confident that I could do the same for Windows 7 64-bit.

I started out by configuring Windows have the IIS6 modules installed, I knew I had to do that. Then I ran the ColdFusion 9 install, no errors, got to the end, "Press OK to start CF Admin", did that. The CF Admin screen came up and did not work. IIS 7 told me about missing extension configuration. I realized at that point something was missed in my IIS reconfiguration, and it turned out it was the ISAPI Extensions under World Wide Web Services options, so I turned all those things on, Windows reconfigured IIS for me. I opened IIS, saw tha the ISAPI extensions and filters were not installed. Ready to go right? Wrong.

Still could not get to CF Admin. Ok, lets uninstall and install CF9 I thought, since now IIS has everything it needs for proper configuration. So I did that, uninstall then install and once again pressed "OK" at the end. Out comes the CF Admin page, "Click here" to continue configuration. Yay, victory was my first though. But then I noticed, something was not right. There was no Adobe logo, no images were showing up. Doesn't matter, I was able to open a .cfm page, so I continued with the configuration and then was logged into the Admin. Still no images. By the way, the Admin looks really bad if you can't see images. So, I closed the browser and opened a new IE window and went to //localhost/cfide/administraor/ and IIS 7 gives me an Error!! But thats where the CF Admin is located, but apparently IIS can't handle the request properly.

Sigh, I did some googling. Verified that the ISAPI handlers for .cfm extensions were pointing to the right DLL. Someone suggested uninstalling CF, deleting all CF directories and installing again. So I dd that, but same error again. Still can't see images in CF Admin. Frustrating, already installed CF9 3 times. Verified IIS setup again, all looks good there. I uninstall CF9 again, restart computer, delete directories, install CF9 again and same issue again. At that point, google had helped me as much as it can. I try to use the server configuration tool but I can't remove the current flawed IIS configuration from it (some error about not able to remove things).

Decide it time to start from scratch. So, I uninstall CF9, delete CF directories, restart computer. Uninstall IIS7 from windows, restart computer. Then reinstall IIS7 and restart computer. Then reinstall CF9 and Victory!! Everything works properly. Apparently, installing the ISAP Extensions after installing CF9 screwed up the installs and configurations in IIS. So, well, it worked now. Logged into CF Admin, //localhost/cfide/administrator/ resolved properly.

But that was not the End of the Story.

Apparently, setting up a MS Access Datasource on Windows 64-bit is another issue faced by various users. You would get an error about DRIVERPATH not defined. Well I thought, if CF can't set it up, I will set it up in Windows and use ODBC socket. But the issue is that the 64-bit Windows don't come with the relevant drivers needed to setup Access Datasource in Windows. After trying to figure out how to resolve this, googling and finding some information here and there, not much help was found. In the end it was realized, that the Windows\System32\odbcad32.exe file contained the proper drivers but the Windows\SysWOW64\odbcad32.exe did not contain the drivers. So the solution was to copy over the .exe from System32 to SysWOW64. I went through with that and it worked!! Even though, ColdFusion still gave a DRIVERPATH undefned error, the datasource was verified properly and I could run all my MS Access based demos.

Whew, finally, was done with setting up Coldfusion 9 64-bit on Windows 7 64-bit.

ExtJS 3.0 Editor Grid with ColdFusion + Ext Toolbar Play

I hadn't looked at the Ext JS library in a while, but this weekend, I took a look at it with the idea of eventually building out a full CRUD grid using Ext JS and ColdFusion. It took some time, particularly trying to figure out the phantom records implementation, but its done now.

View the Demo

And as always, time to look at the code.

[More]

Using jQuery based jqGrid with ColdFusion Revisited: Full CRUD grid with Advanced Searching

It has been a while since I made my last blog post as work has kept me very busy. Over the weekend I saw that a new jqGrid version had been released and that it contained in-built advanced searching (called multiple search). I also thought it was a good time to revisit my earlier blog post on a full CRUD grid and update it to 3.6 specifications.


This implementation uses the following jqGrid features:

  1. Inbuilt Form based Add, Edit, Delete and Search functionality
  2. Search is done using new Multiple Search feature
  3. Exploration of a few additional colModel parameters

View the Demo : this is all realtime, any updates made will be reflected in the grid.
Now, lets look at the code

[More]

Playing with jQuery UI : Part 1

I downloaded the jQuery UI library a few weeks back (should have done it earlier too). I started playing around with it, but work has been so busy did not get much time to look at it. Finally, found some time to do a basic tabs, accordians, sliders, progress bar demo. Its very basic, good start to learing how some of the things work.

View Demo

The progress bar demo is probably the most complex one, so code for it follows.

[More]

Registration for CFinNC Conference Open (Free ColdFusion based Conference)

I am personally from Charlotte, so when the CFinNC Conference was announced, it was very exciting. I wanted to get to CFUnited this year but could not make it because of a very busy work schedule, so a weekend conference a few hours away was awesome. And on top of everything it is Free. Let me say that again, it is Free.

They put out a provisional list of speakers and its a very good list. A lot of people who I wanted to listen to in previous conferences will be there.

So head on over to cfinnc.com and register for it.

jqGrid 3.5 with jQuery UI ThemeRoller Themes Demos

jqGrid 3.5 was released yesterday. It includes lots of good features, including a new rendering engine for faster loading. It also now supports jQuery UI ThemeRoller themes, so to start off itself you have access to variety of styles and can create your own easily too.

So I went ahead and created a demo that shows the grid in all these themes.
View Demo

How I did this style selection switch, follows

[More]

More Entries

/* ChartBeat Testing */