Solved

SQL - no duplicate row of combination of ColumnA and ColumnB

Posted on 2010-09-08
2
561 Views
Last Modified: 2012-05-10
This query is for quality check on a TableAB- query checks that there is no duplicate row with the same values of ColumnA and ColumnB.
Example:
ColumnA                    ColumnB              ColumnC             ColumnD
2                                   6                     22                   23
2                                   6                     26                    42

This case above should not happen. The query needs to return such records if they exist.
0
Comment
Question by:spirose
2 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 33632109
SELECT ab.ColumnA, ab.ColumnB, ab.ColumnC, ab.ColumnD
FROM TableAB ab INNER JOIN
    (SELECT ab2.ColumnA, ab2.ColumnB
    FROM TableAB ab2
    GROUP BY ab2.ColumnA, ab2.ColumnB
    HAVING COUNT(1) > 1) AS z ON ab.ColumnA = z.ColumnA AND ab.ColumnB = z.ColumnB
ORDER BY ab.ColumnA, ab.ColumnB, ab.ColumnC, ab.ColumnD
0
 

Author Comment

by:spirose
ID: 33632426
Works great- thank you.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now