Avatar of shieldguy
shieldguy
Flag for United Kingdom of Great Britain and Northern Ireland asked on

How to get the only repeating record from the table

I have a table how can i return only the repeating records in the table where productID and orderID column sboth have repeating values both are same.

Table structure:
Name : returnproducts

columns :
returnproductid
dateadded
productid
orderid

Thanks
Thanks
Microsoft SQL ServerMicrosoft SQL Server 2005SQL

Avatar of undefined
Last Comment
shieldguy

8/22/2022 - Mon
chapmandew

select productid, orderid
from tablename
group by productid, orderid
having count(*) > 1
ASKER CERTIFIED SOLUTION
Aneesh

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
ee_rlee

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
shieldguy

ASKER
chapmandew: Query given me 12 records

aneeshattingal: query given me 30 records

which one is correct

chapmandew

it depends on what you want.

My query gives you a unique list of products and orders that appear more than one time

aneeshattingal's query gives you the list of all records where that combination appears more than one time.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
shieldguy

ASKER
actually i also need to find out the returnproductid which should be unique anyway
Aneesh

>actually i also need to find out the returnproductid which should be unique anyway
that means there are 30 records  :)
chapmandew

Then this should work:

select productid, orderid, MIN(returnproductid)
from tablename
group by productid, orderid
having count(*) > 1
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
chapmandew

it doesn't mean that there are 30 unique records though.
shieldguy

ASKER
actually whats happening while inserting the records in to this table some how the records with same productid and orderid are entering in to the database and so now i need to filter them all out and delete them
chapmandew

All of them, or all of them, but one?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
chapmandew

do you have a field that uniquely identifies the records in your table (primary key)?
shieldguy

ASKER
returnproductid is the primary key

also if we have 3 records of product id = 33
and orderid = 44
then i need to see all these 3 records with their returnproductid beacuse returnproductid will be uniquie for all 3 of them and so i can delete them


ee_rlee

You can try my last post or change aneeshattingal's solution to:

SELECT r.*
FROM ...
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
chapmandew

If you want to delete all 3 records, then:

delete r
from returnproducts r
join
(
select productid, orderid
from tablename
group by productid, orderid
having count(*) > 1
) a on r.productid = a.productid and r.orderid = a.orderid
ee_rlee

This would delete records with duplicate productid and orderid except one (min returnproductid)

DELETE R FROM returnproducts R
WHERE returnproductid<> (SELECT MIN(returnproductid) FROM returnproducts WHERE productID=R.productID and orderID=R.orderID)
shieldguy

ASKER
thanks
Your help has saved me hundreds of hours of internet surfing.
fblack61