?
Solved

SQL Query

Posted on 2010-01-08
6
Medium Priority
?
330 Views
Last Modified: 2012-05-08

I need to write a query to check for duplicates in data..

Columns of table
ItemNumber,  ProductNumber, Country, List, Contract, DateCode, Cdate, AddDate, ModifyDate


The conditions for duplicates is that ItemNumber,  ProductNumber,Cdate,
Must be unique

I Need to select these records



0
Comment
Question by:Leo Torres
  • 3
  • 3
6 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 26212442
select * from (
select ranking = dense_rank() over(partition by itemnumber, productnumber, cdate order by newid())
from tablename
) a
where ranking > 1

shows you the dupes.
0
 
LVL 8

Author Comment

by:Leo Torres
ID: 26212563
Ok Cool.. this only selects the rank I need to select the complete row of the table to see what info is dup

Sorry i did not metion name of table below is the query I ran

thanks

select * from (
select ranking = dense_rank() over(partition by itemnumber, productnumber, cdate order by newid())
from Pricing
) a
where ranking > 1

Open in new window

0
 
LVL 60

Expert Comment

by:chapmandew
ID: 26212590
sorry...forgot to get the rest:

select * from (
select ranking = dense_rank() over(partition by itemnumber, productnumber, cdate order by newid()), *
from tablename
) a
where ranking > 1
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 8

Author Comment

by:Leo Torres
ID: 26212751
That great.. I verified the data.. Now I tried to run a delete but i wont work

Errors
Server Msg: 102, State: 42000, [Microsoft][SQL Server Native Client 10.0][SQL Server]
Incorrect syntax near '('.
Server Msg: 102, State: 42000, [Microsoft][SQL Server Native Client 10.0][SQL Server]
Incorrect syntax near 'a'.

is it that much different to delete??
Delete from (
select ranking = dense_rank() over(partition by itemnumber, productnumber, cdate order by newid()), *
from Pricing
) a
where ranking > 1

Open in new window

0
 
LVL 60

Accepted Solution

by:
chapmandew earned 2000 total points
ID: 26212762
try this:

with cte
as
(
select * from (
select ranking = dense_rank() over(partition by itemnumber, productnumber, cdate order by newid()), *
from Pricing
) a
)

delete from cte
where ranking > 1
0
 
LVL 8

Author Comment

by:Leo Torres
ID: 26212783
WOW.... That's freakin cool.. Thanks worked like a charm!!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How much do you know about the future of data centers? If you're like 50% of organizations, then it's probably not enough. Read on to get up to speed on this emerging field.
Among the most obnoxious of Exchange errors is error 1216 – Attached Database Mismatch error of the Jet Database Engine. When faced with this error, users may have to suffer from mailbox inaccessibility and in worst situations, permanent data loss.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…

621 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