Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 880681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:08:18+00:00 2026-05-15T12:08:18+00:00

Using SQL Server 2005 I have a table with the following columns id name

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T12:08:19+00:00Added an answer on May 15, 2026 at 12:08 pm

    This is assuming you ordered the rows by ID but you can simply change the ORDER BY id to 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.

    WITH CTE as (
      SELECT
        RN = ROW_NUMBER() OVER (ORDER BY id),
        *
      FROM tablename
    )
    SELECT
      [Current Row].*
    FROM CTE [Current Row]
    LEFT JOIN CTE [Previous Row] ON
      [Previous Row].RN = [Current Row].RN - 1
    LEFT JOIN CTE [Next Row] ON
      [Next Row].RN = [Current Row].RN + 1
    WHERE
      not([Current Row].value = 0 AND [Next Row].value = 0) AND  
         // this deletes the row where value is zero and the next rows value is zero
      not([Previous Row].value = 0 AND [Current Row].value = 0) 
         // this deletes the row where value is zero and the previous rows value is zero
    

    All you have to do to make it work for your case is putting each possible combination within the WHERE statement. For example dealing with this row and the 3 next rows equaling 0 OR this row the previous and the 2 next rows.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using SQL-Server 2005 Standard. I have Users table with following columns: userID(int), userImage(varchar),
I have a table (SQL Server 2005) with the following columns: State: - StateId
HI, Using SQL server 2005 I have the following query: SELECT contact_id ,YEAR(date_created) AS
For instance, my query is like the following using SQL Server 2005: SELECT *
[using SQL Server 2005] I have a table full of users, I want to
I'm using SQL Server 2005 express I have a datetime field in a table
Im using SQL Server 2005. I have a table1 with 3 columns. And table2
I'm using SQL Server 2005 and have the following T-SQL statement: DECLARE @MP VARCHAR(500)
I am using SQL Server 2005. I have a requirement to list out all
I'm using SQL Server 2005. I have a table whose row size should be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.