Using SQL Server 2005
I have a table with the following columns
id
name
date
value
I would like to select all rows from the table where there are not four consecutive zeroes by date. How would I do that? Below is an example of what I mean.
id name date value
1 a 1/1/2010 5
2 a 1/2/2010 3
3 a 1/3/2010 5
4 a 1/4/2010 0
5 a 1/7/2010 0
6 a 1/8/2010 0
7 a 1/9/2010 2
8 a 1/10/2010 3
9 a 1/11/2010 0
10 a 1/15/2010 0
11 a 1/16/2010 0
12 a 1/17/2010 0
13 a 1/20/2010 4
14 a 1/21/2010 4
I would like the result of the query to include all rows except id 9-12.
This is assuming you ordered the rows by ID but you can simply change the
ORDER BY idto something else and it should still work.Using the T-SQL CTE found on this Kodyaz Development Resources site I was able to create the below code. I have it working so it deletes the rows where there are two consecutive zeroes, not 4, as I tested it on my code and just changed the table/row names.
All you have to do to make it work for your case is putting each possible combination within the
WHEREstatement. For example dealing with this row and the 3 next rows equaling 0 OR this row the previous and the 2 next rows.