Solved

grouping sequenced data

Posted on 2009-07-08
6
220 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
Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
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…

710 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