Drop Table

Support Forum for database administrators and web based access to important newsgroups related to databases
Register on Database Support Forum Edit your profileCalendarFind other Database Support forum membersFrequently Asked QuestionsSearch this forum -> 
For Database admins: Free Database-related Magazines Now Free shipping to Texas


Post New Thread










Thread
Author

How to Determine the unique IDs of duplicated records
>  This is a common problem with some  solution
>
>

/ ********************
 ********************
 ********************
***************
********
*
*  Problem:
*  Determine the Duplicated Records in a table using single SELECT.
*
*  We shall be using Northwind database, add some duplicate records.
*
*  Here we want to  know if  2 columns (CompanyName,
*  PHone) are duplicated in a table.
*
*
* ShipperID   CompanyName                    Phone
* ----------- -------------------------   ------------------
* 1           Speedy Express                     (503) 555-9831
* 2           United Package                     (503) 555-3199
* 3           Federal Shipping                   (503) 555-9931
* 4           Federal Shipping                   (503) 555-9931
* 5           Speedy Express                     (503) 555-9831
* 6           Federal Shipping                   (503) 555-9931
*
*
*
 ********************
 ********************
************/

 ====================
 ====================
==========

SOLUTION 1:  Gives me the IDs that are duplicated.

 ====================
 ====================
==========

SELECT
ShipperID,  CompanyName,  Phone
FROM
SHIPPERS
WHERE
EXISTS  (
SELECT
NULL
FROM
SHIPPERS b
WHERE
b.CompanyName = SHIPPERS.CompanyName
AND b.Phone       = SHIPPERS.Phone
GROUP BY
b.CompanyName, b.Phone
HAVING
SHIPPERS.ShipperID < MAX( b.ShipperID )
)

/*  ********************

* Output results
 ********************
/

ShipperID   CompanyName                              Phone

----------- ----------------------------------------
------------------------
1           Speedy Express                           (503) 555-9831
3           Federal Shipping                         (503) 555-9931
4           Federal Shipping                         (503) 555-9931

(3 row(s) affected)



 ====================
 ====================
 ====================
=

SOLUTION 2:  Gives me the data which are duplicate but
not the IDs

 ====================
 ====================
 ====================
=


SELECT
CompanyName, Phone
FROM
SHIPPERS
GROUP BY
CompanyName, Phone
HAVING
COUNT(*) > 1


/*  ********************

* Output results
 ********************
/



CompanyName                              Phone
---------------------------------------- ------------------------
Speedy Express                           (503) 555-9831
Federal Shipping                         (503) 555-9931

(2 row(s) affected)


Report this thread to moderator Post Follow-up to this message
Old Post
anonieko@hotmail.com
02-25-06 02:44 PM


Re: How to Determine the unique IDs of duplicated records
anonieko@hotmail.com  wrote: 
>
> / ********************
 ********************
 ********************
*************
**********
>  *
>  *  Problem:
>  *  Determine the Duplicated Records in a table using single SELECT.
>  *
>  *  We shall be using Northwind database, add some duplicate records.
>  *
>  *  Here we want to  know if  2 columns (CompanyName,
>  *  PHone) are duplicated in a table.
>  *
>  *
>  * ShipperID   CompanyName                    Phone
>  * ----------- -------------------------   ------------------
>  * 1           Speedy Express                     (503) 555-9831
>  * 2           United Package                     (503) 555-3199
>  * 3           Federal Shipping                   (503) 555-9931
>  * 4           Federal Shipping                   (503) 555-9931
>  * 5           Speedy Express                     (503) 555-9831
>  * 6           Federal Shipping                   (503) 555-9931
>  *
>  *
>  *
>   ********************
 ********************
************/
>
>  ====================
 ====================
==========
>
> SOLUTION 1:  Gives me the IDs that are duplicated.
>
>  ====================
 ====================
==========
>
> SELECT
>    ShipperID,  CompanyName,  Phone
> FROM
>    SHIPPERS
> WHERE
>   EXISTS  (
>     SELECT
>       NULL
>     FROM
>       SHIPPERS b
>     WHERE
>           b.CompanyName = SHIPPERS.CompanyName
>       AND b.Phone       = SHIPPERS.Phone
>     GROUP BY
>       b.CompanyName, b.Phone
>     HAVING
>       SHIPPERS.ShipperID < MAX( b.ShipperID )
>    )
>
> /*  ********************

>  * Output results
>   ********************
/
>
>  ShipperID   CompanyName                              Phone
>
>  ----------- ----------------------------------------
> ------------------------
>  1           Speedy Express                           (503) 555-9831
>  3           Federal Shipping                         (503) 555-9931
>  4           Federal Shipping                         (503) 555-9931
>
>  (3 row(s) affected)
>
>
>
>  ====================
 ====================
 ====================
=
>
> SOLUTION 2:  Gives me the data which are duplicate but
>                        not the IDs
>
>  ====================
 ====================
 ====================
=
>
>
> SELECT
>    CompanyName, Phone
> FROM
>    SHIPPERS
> GROUP BY
>    CompanyName, Phone
> HAVING
>    COUNT(*) > 1
>
>
> /*  ********************

>  * Output results
>   ********************
/
>
>
>
>    CompanyName                              Phone
> ---------------------------------------- ------------------------
> Speedy Express                           (503) 555-9831
> Federal Shipping                         (503) 555-9931
>
> (2 row(s) affected)


Those aren't solutions, they are diagnostics. The solution is to fix
the stupid design of the Shippers table by adding a proper key.

:-)

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--


Report this thread to moderator Post Follow-up to this message
Old Post
David Portas
02-25-06 02:44 PM


Sponsored Links





Last Thread Next Thread
Post New Thread

Microsoft SQL Server forum archive

Show a Printable Version Email This Page to Someone! Receive updates to this thread
Microsoft SQL Server
Access database support
PostgreSQL Replication
SQL Server ODBC
FoxPro Support
PostgreSQL pgAdmin
SQL Server Clustering
MySQL ODBC
Web Applications with dBASE
SQL Server CE
MySQL++
Sybase Database Support
MS SQL Full Text Search
PostgreSQL Administration
SQL Anywhere support
DB2 UDB Database
Paradox Database Support
Filemaker Database
Berkley DB
SQL 2000/2000i database
ASE Database
Forum Jump:
All times are GMT. The time now is 04:25 AM.

 
Mobile devices forum | Database support forum archive




Copyrights DropTable.com Database Support Forum 2004 - 2006