?
Solved

Getting the Consecutive records

Posted on 2008-10-21
6
Medium Priority
?
217 Views
Last Modified: 2010-04-21
Hi All,
 I have table with colum Id (Identity column) and return .
The table data look like below
ID    Returns
1     0.0
2     1.2
3.    0.0
4     0.0
5    0.0
6   2.3
7  0.0
8  0.0
9  3.4

In the above table, I only want the record which have Returns as zero for three consecutively rows.
In the above table, i need a query i will select only the record 3,4 and 5.I dont want to use loops. Please help me.
0
Comment
Question by:Jeyakumar_mcp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22767081
clarify, please:
sql 2000 or sql 2005+?
0
 
LVL 1

Author Comment

by:Jeyakumar_mcp
ID: 22767192
it is in sql 2000
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 150 total points
ID: 22767372
ok, what about this:
select t1.id, t2.id, t3.id
  from yourtable t1
  join yourtable t2
    on t2.id = t1.id + 1
   and t2.returns = t1.returns
  join yourtable t3
    on t3.id = t3.id + 1
   and t3.returns = t1.returns
  where t1.returns = 0.0

Open in new window

0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 1

Author Comment

by:Jeyakumar_mcp
ID: 22767562
HI,
  It is not working.
For the input
The table data look like below
ID    Returns
1     0.0
2     1.2
3.    0.0
4     0.0
5    0.0
6   2.3
7  0.0
8  0.0
9  3.4

I need the output as below
ID    Returns
3.    0.0
4     0.0
5    0.0

Please advice
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22767799
does my query return 1 rows with the 3 id values?

if yes:
select t.* 
  from yourtable t
  join ( select t1.id id1, t2.id id2, t3.id id3
  from yourtable t1
  join yourtable t2
    on t2.id = t1.id + 1
   and t2.returns = t1.returns
  join yourtable t3
    on t3.id = t3.id + 1
   and t3.returns = t1.returns
  where t1.returns = 0.0
  ) sq
  on t.id in (sq.id1, sq.id2, sq.id3)

Open in new window

0
 
LVL 1

Author Closing Comment

by:Jeyakumar_mcp
ID: 31508259
Thanks Angel. It works fine  with slight modification. Thanks a lot
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question