SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 DELETE RECORDS ON LESS THAN TWO EXIST
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

jbbatts77
Starting Member

2 Posts

Posted - 03/08/2013 :  14:57:56  Show Profile  Reply with Quote
Hello,

I have a table that has numerical data in it like so:

3
3
3
3
5
5
5
5
6
6
7
7
7
7
8
9
9
9

How do I delete all records where only 2 or less instances exist? In the above case it would be the two 6's and the 8

Bustaz Kool
Flowing Fount of Yak Knowledge

USA
1600 Posts

Posted - 03/08/2013 :  19:33:13  Show Profile  Reply with Quote
delete MyTable
where MyColumn in (
   select MyColumn
   from MyTable
   group by MyColumn
   having count(*) <= 2
   )


=================================================
There are two kinds of light -- the glow that illuminates, and the glare that obscures. -James Thurber
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 03/09/2013 :  00:58:37  Show Profile  Reply with Quote

delete t
from
(
select cout(1) over (partition by yourcol) AS cnt
from table
)t
where cnt<=2


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000