Sometimes awesome things just happen. Today Rob Farley (@Rob_Farley) was helping me with a previous post about my dates table and just as a side note he said, “Oh, let me show you something else cool.” It was really cool and so I asked if I could add it to my blog since I never know when this problem will strike.
I have a table about Super Heroes and their cape colors. I made a mistake and put Batman in there twice. But since there is no primary key, how do I tell it which one to delete?
Since they are exactly the same, I can let SQL sort it out. This uses both a CTE (I hadn’t ever used it without some kind of join before today) and OVER which I am learning about. So cool!
WITH SuperHeroDuplicates AS (SELECT *, ROW_NUMBER() over (partition by HeroName,CapeColor order by HeroName,CapeColor) as rownum FROM dbo.HeroCapeColor) DELETE FROM SuperHeroDuplicates where rownum > 1;
Kenneth Fisher (@sqlstudent144) also wrote a blog post about another way you can accomplish this task if you only have a few of them to delete.
He has been a super big help to me. He taught me how to display my code better in my blog so it is easier to read and copy. He also encourages me, builds my confidence and even included a link to one of my posts in an article. It made me feel special and like what I have to say matters to other people. I love how he comments on my posts and gives me ideas on how to make them better. I feel so lucky to be a part of such a great community of people that are so thoughtful, selfless and giving. Thanks Kenneth for being such a great example to me!