Solved

Getting the Consecutive records

Posted on 2008-10-21
6
210 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 142

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 142

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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
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 142

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

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server 2000: == Linked Server, With higher version == 16 114
How to print the sql query 18 52
Pivot Query Problem 9 38
Replace Dates in query 14 20
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…
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 is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

864 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now