Solved

grouping sequenced data

Posted on 2009-07-08
6
188 Views
Last Modified: 2013-11-05
From the table

Start_date    end_date  value
2009/01/01  2009/30/04   A
2009/01/05  2009/31/06   A
2009/01/06  2009/31/08   B
2009/01/09  2009/31/12   A

I need the table
2009/01/01  2009/31/05   A
2009/01/06  2009/31/08   B
2009/01/09  2009/31/12   A

Werner

0
Comment
Question by:WAeberhardt
6 Comments
 

Expert Comment

by:cjonline
ID: 24801869
Can you explain the ordering?
0
 
LVL 20

Expert Comment

by:ChristoferDutz
ID: 24801876
I doubt this will be possible using standard SQL since in SQL statements there is no such concept of "previous record" or "next record" ... I think you will have to do the processing in the programming language that you are using to access the database.
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 24803114
if your database supports LEAD and LAG you can do this with sql.

I think there is a problem in your sample data...

2009/31/06   should be
2009/31/05  I think,  based on your expected results.

I've tested the query below in Oracle and it produces the expected results.
SELECT   DISTINCT

         CASE WHEN start_date - 1 <= prevend THEN prevstart ELSE start_date END start_date,

         CASE WHEN end_date + 1 >= nextstart THEN nextend ELSE end_date END end_date,

         VALUE

FROM     (SELECT start_date,

                 end_date,

                 VALUE,

                 LAG(start_date) OVER (PARTITION BY VALUE ORDER BY start_date) prevstart,

                 LAG(end_date) OVER (PARTITION BY VALUE ORDER BY start_date) prevend,

                 LEAD(start_date) OVER (PARTITION BY VALUE ORDER BY start_date) nextstart,

                 LEAD(end_date) OVER (PARTITION BY VALUE ORDER BY start_date) nextend

          FROM   your_table) x

ORDER BY start_date, end_date, VALUE

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.

 

Author Comment

by:WAeberhardt
ID: 24804249
sdstuber,

Regarding data, You are right. (mistake in my sample)

Can You give me the syntax of the LAG and LEAD scalar functions? I'll try to write them for my dialect.
Werner

0
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
ID: 24804337
the syntax is in the code snippet

if your database doesn't support analytics you probably won't be able to write them

Instead you will probably need to do self-joins to find previous/next values
or iterate through the values with a procedural language.
0
 

Author Comment

by:WAeberhardt
ID: 24808124
Two benefits of answer

firstly, it was important to know, that there is no solution in standard SQL
secondly, the path to solve the problem in now clear (working with cursor. )
--> I can rebuild the solution in my dialect.

Werner
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

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This article describes some very basic things about SQL Server filegroups.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

19 Experts available now in Live!

Get 1:1 Help Now