Reputation: 11
I have a database that needs filtering.
The database contains rows of employees at various companys who have signed up to a newsletter. I don't wish to send the newsletter to each one, I only want to send it to 2 from each company. what would my SQL be to remove the duplicates if more than 2 from 1 company listed?
Thanks for any help.
Upvotes: 1
Views: 142
Reputation: 890
May be this will help you...
DELETE FROM newsletter nl
WHERE ID NOT IN
(
select id from newsletter ne where ne.comp_name=hl.comp_name limit 2
)
I am using table name as examples just replace it with your original name.
Upvotes: 2
Reputation: 1774
Try this query. It is for Sql Server database.
select * from Employees e
where id in (select top 2 id from Employees ie where e.company = ie.company)
Please replace the table and field names as the exact structure of table is not known.
Upvotes: 1