Solved

Getting the Consecutive records

Posted on 2008-10-21
6
209 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

759 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

22 Experts available now in Live!

Get 1:1 Help Now