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
Solved

Getting the Consecutive records

Posted on 2008-10-21
6
213 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
  • 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 50 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

809 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