Solved

grouping sequenced data

Posted on 2009-07-08
6
207 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
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…

777 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