Solved

grouping sequenced data

Posted on 2009-07-08
6
225 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
[X]
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
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 74

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
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.

 

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 74

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

632 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