Question

How to remove the overlapping data?

Hi,
      I have data like below.
MODEL           PRICE            START_DATE_ACTIVE            END_DATE_ACTIVE
SONY             100                         1/1/1901                           12/31/2005
SONY             200                         1/1/2006                           12/31/2007
SONY             300                         1/1/2008                           12/31/2008
SONY             400                         2/1/2008                           12/31/2009
SONY             500                         1/1/2009                           12/31/2010
                     The above the model SONY is having the price range that depends on the start_date_active and end_date_active column. For example in the first row, the model SONY price is 100 from 1/1/2006 to 12/31/2007. The same model SONY price is 200 from 1/1/2008 to 12/31/2008. So the model price will come like this. But in some cases the date will be overlapping. See the row number 3 and 4. I should remove the rownumber 4. How can i remove the overlapping date data?

Thanks

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-03-30 at 03:53:01ID24276804
Tags

Oracle

,

SQL

,

PL/SQL

Topic

Oracle 9.x

Participating Experts
2
Points
500
Comments
37

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Detect Control Overlapping
    hi, i am doing a project in VB. i need my code to check if any of my image controls added during run time overlap. can any one please help me.
  2. Overlapping Columns
    Hi, In my Excel spreadsheet, one of the columns (With a lot of data) has the wording/data extending out to the right, covering over the columns that follow, and blocking the data underneath the other columns. How can I keep the data in that column from extending over the ot...
  3. Overlapping content in floated column
    I have a website that lays out three main columns using float:right. The layout works well except when the center content is short then content in the outer columns overlaps the footer, rather than stretching all three columns. It looks really odd and i have tried all sorts o...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: sdstuberPosted on 2009-03-30 at 16:42:11ID: 24024547

try this....

DELETE FROM   your_table
      WHERE   ROWID IN
                      (SELECT   rid
                         FROM   (SELECT   ROWID rid,
                                          start_date_active sda,
                                          end_date_active eda,
                                          LAG(end_date_active)
                                              OVER (
                                                  PARTITION BY model
                                                  ORDER BY start_date_active, end_date_active
                                              )
                                              prevend,
                                          LEAD(start_date_active)
                                              OVER (
                                                  PARTITION BY model
                                                  ORDER BY start_date_active, end_date_active
                                              )
                                              nextstart
                                   FROM   your_table)
                        WHERE   sda < prevend AND eda > nextstart)

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:

Select allOpen in new window

 

by: Suriyaraj_SudalaiappanPosted on 2009-03-30 at 23:29:17ID: 24026180

Hi thanks for your reply. It is returning no rows.

 

by: Sharath_123Posted on 2009-03-30 at 23:33:08ID: 24026194


Can you clarify:
Row number 3 and 4 are overlapping but rownumber 4 exceeds the end dat of rownumber 3. Also rownumber 4 and 5 are overlapping but rownumber 5 end date exceeds the end date of row number4.
What is your ouptut if you don't have rownumber 5 in your sample set?

 

by: Suriyaraj_SudalaiappanPosted on 2009-03-31 at 04:16:47ID: 24027551

Hi sdstuber, i added one more condition in this query. It is not fetching any rows. In my table, i have 10000 rows. I want to see only the particular item. So i have added one AND condition in that query like below. Please verify my below query is correct or not.

SELECT * FROM   sample_table
      WHERE   ROWID IN
                      (SELECT   rid
                         FROM   (SELECT   ROWID rid,
                                          new_start_date sda,
                                          new_end_date eda,
                                          LAG(new_end_date)
                                              OVER (
                                                  PARTITION BY ITEM_NAME
                                                  ORDER BY new_start_date, new_end_date
                                              )
                                              prevend,
                                          LEAD(new_start_date)
                                              OVER (
                                                  PARTITION BY ITEM_NAME
                                                  ORDER BY new_start_date, new_end_date
                                              )
                                              nextstart
                                   FROM   sample_table)
                        WHERE   sda < prevend AND eda > nextstart)                                                
                        AND ITEM_NAME = 'PHILIPS'

 

by: sdstuberPosted on 2009-03-31 at 04:23:03ID: 24027585

that will work or you can put it in the inner query

SELECT   *
  FROM   sample_table
 WHERE   ROWID IN
                 (SELECT   rid
                    FROM   (SELECT   ROWID rid,
                                     new_start_date sda,
                                     new_end_date eda,
                                     LAG(new_end_date)
                                         OVER (
                                             PARTITION BY item_name
                                             ORDER BY new_start_date, new_end_date
                                         )
                                         prevend,
                                     LEAD(new_start_date)
                                         OVER (
                                             PARTITION BY item_name
                                             ORDER BY new_start_date, new_end_date
                                         )
                                         nextstart
                              FROM   sample_table
                             WHERE   item_name = 'PHILIPS')
                   WHERE   sda < prevend AND eda > nextstart)

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:

Select allOpen in new window

 

by: Suriyaraj_SudalaiappanPosted on 2009-03-31 at 04:23:30ID: 24027589

Hi Sharath,
      you are correct. In my case overlapping means, it should not start with same year again with another row. For the above scenario, rownum 3 is ended with 12/31/2008. So that particular model price is deactivated after this date. So it has to start the new price from  1/1/2009  date. So nownum 5 is valid. In between rownum 3 and 5, again the start date started from the year which is already mentioned in rownum 3. So we should not consider this right. I thing now you can understand right.

Thanks

 

by: Suriyaraj_SudalaiappanPosted on 2009-03-31 at 09:42:58ID: 24030827

Hi sdstuber,
         If i change the condition like below, it is giving the overlapping data for me. See my attached file for my actual data.
WHERE   sda < prevend AND eda < nextstart). Can you tell me what is the reason?

 

by: sdstuberPosted on 2009-03-31 at 10:08:38ID: 24031106

the problem was your original sample violated both ends
so,  sda < prevend AND eda > nextstart  found it.

but your new sample only violates on one end.
since all "good data will have eda < nextstart  that's not really a helpful condition.
so  sda < prevend AND eda < nextstart
would fail to find bad data like your original sample


 

by: sdstuberPosted on 2009-03-31 at 10:12:45ID: 24031149

changing the AND to OR will work except then you'll get 2 rows returns for each overlap
because a violation on either end will guarantee the other row will fail on the other end.

from your last example

2008/12/31 (end) on line 6 > 2008/12/01 (start) on line 7 so that's an overlap
but by the same logic  
2008/12/01 (start) on line 7 < 2008/12/31 (end) on line 6 so that's an overlap

 

by: sdstuberPosted on 2009-03-31 at 10:15:29ID: 24031189

we could check on just one end, but then if you have cases like in your original example then you'll get false positives around the problem row.

To illustrate, from your first example, the 400 line is the problem.
if we check only the end then the 300 line will show up as a problem as well as the 400 line.
if we check only the start then the 500 line will show up as a problem as well as the 400 line

SONY             300                         1/1/2008                           12/31/2008
SONY             400                         2/1/2008                           12/31/2009
SONY             500                         1/1/2009                           12/31/2010

 

by: Suriyaraj_SudalaiappanPosted on 2009-03-31 at 10:27:06ID: 24031332

I don't want to remove 2 overlapping records. I want remove only the line number 7. becasue that start date is not ended to other rows. For example, if the end_date is 2008/12/31, the next row should start from 1/1/2009. But line number 6 is starting with 2008/12/01 right. So we need to consider this as a overlap. So i need to remove only those rows. But my first example also applicable for some cases. It should ended with after the end date of previous row. We need to consider both the cases.

 

by: Suriyaraj_SudalaiappanPosted on 2009-03-31 at 10:28:09ID: 24031345

Unless i can give only like below right.

WHERE   sda < prevend

 

by: sdstuberPosted on 2009-03-31 at 10:28:37ID: 24031351

easiest solution is probably to run two deletes...

first delete all rows that have a double violation
then delete rows where the new_start_date < previous row's end date

DELETE FROM sample_table
      WHERE ROWID IN
                    (SELECT rid
                       FROM (SELECT ROWID rid,
                                    new_start_date,
                                    new_end_date,
                                    LAG(new_start_date)
                                        OVER (
                                            PARTITION BY item_name
                                            ORDER BY new_start_date, new_end_date
                                        )
                                        prevstart,
                                    LAG(new_end_date)
                                        OVER (
                                            PARTITION BY item_name
                                            ORDER BY new_start_date, new_end_date
                                        )
                                        prevend,
                                    LEAD(new_start_date)
                                        OVER (
                                            PARTITION BY item_name
                                            ORDER BY new_start_date, new_end_date
                                        )
                                        nextstart,
                                    LEAD(new_end_date)
                                        OVER (
                                            PARTITION BY item_name
                                            ORDER BY new_start_date, new_end_date
                                        )
                                        nextend
                               FROM sample_table
                              WHERE item_name = 'SONY')
                      WHERE new_start_date < prevend AND new_end_date > nextstart);
 
DELETE FROM sample_table
      WHERE ROWID IN
                    (SELECT rid
                       FROM (SELECT ROWID rid,
                                    new_start_date,
                                    new_end_date,
                                    LAG(new_start_date)
                                        OVER (
                                            PARTITION BY item_name
                                            ORDER BY new_start_date, new_end_date
                                        )
                                        prevstart,
                                    LAG(new_end_date)
                                        OVER (
                                            PARTITION BY item_name
                                            ORDER BY new_start_date, new_end_date
                                        )
                                        prevend,
                                    LEAD(new_start_date)
                                        OVER (
                                            PARTITION BY item_name
                                            ORDER BY new_start_date, new_end_date
                                        )
                                        nextstart,
                                    LEAD(new_end_date)
                                        OVER (
                                            PARTITION BY item_name
                                            ORDER BY new_start_date, new_end_date
                                        )
                                        nextend
                               FROM sample_table
                              WHERE item_name = 'SONY')
                      WHERE new_start_date < prevend );
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:

Select allOpen in new window

 

by: sdstuberPosted on 2009-03-31 at 10:31:41ID: 24031378

you can't "just" do the delete where next_start_date < prevend  because rows with double violation will cause a double row delete.  themselves plus the following row.

that's why I suggest doing it in two steps.

 

by: Suriyaraj_SudalaiappanPosted on 2009-03-31 at 10:45:20ID: 24031543

Ya it was great. I am putting those two queries in union and it is working for both condition. But if i make it as UNION, how should i delete?

 

by: sdstuberPosted on 2009-03-31 at 10:48:00ID: 24031581

don't combine them.

do two deletes

if you do both at once then you'll get extra deletes like I described above.

first delete the rows that violate on both ends
then delete the rows that violate on one end.

 

by: Suriyaraj_SudalaiappanPosted on 2009-03-31 at 10:48:28ID: 24031591

I mean how should i delete those records from the table?

 

by: sdstuberPosted on 2009-03-31 at 10:50:22ID: 24031613

if you want to try to combine them then you'll have to write a much more complicated set of filters in order to determine if rows that violate on only one end should be kept anyway because their violation is caused by another row's double ended violation.

much simpler to just do two deletes

 

by: sdstuberPosted on 2009-03-31 at 10:51:01ID: 24031623

oh sorry, didn't see you posted again.

how to delete?
with the two deletes I posted above.

 

by: Sharath_123Posted on 2009-03-31 at 15:06:07ID: 24034205

can you try this?

select t1.MODEL,t1.PRICE,t1.START_DATE_ACTIVE,t1.END_DATE_ACTIVE
  from (select *,ROW_NUMBER() over (partition by Model order by Price) RN from your_table) t1
  left join (select *,ROW_NUMBER() over (partition by Model order by Price) RN from your_table) t2
    on t1.Model = t2.Model and t1.RN - 1 = t2.RN
  left join (select *,ROW_NUMBER() over (partition by Model order by Price) RN from your_table ) t3
    on t1.Model = t3.Model and t1.RN + 1 = t3.RN
 where case when (t1.START_DATE_ACTIVE < t2.END_DATE_ACTIVE) and 
                 (t1.END_DATE_ACTIVE > t3.START_DATE_ACTIVE) and 
                 (t1.END_DATE_ACTIVE < t3.END_DATE_ACTIVE)  then 1 else 0 end = 0
 order by t1.Model,t1.RN

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:

Select allOpen in new window

 

by: sdstuberPosted on 2009-03-31 at 15:16:30ID: 24034314

why query the table 3 times?

I don't understand the strategy behind the query,
I do understand the functionality of it, just not the why.


but, speaking of "why",  in my previously posted deletes, I included a couple of extra analytics that aren't really necessary.
I was experimenting and didn't take them out before posting.  They aren't harmful but aren't helpful either.

here are cleaner versions.  I'll admit, still "bigger" code than the previous post, but it should be much more efficient.

DELETE FROM sample_table
      WHERE ROWID IN
                    (SELECT rid
                       FROM (SELECT ROWID rid,
                                    new_start_date,
                                    new_end_date,
                                    LAG(new_end_date)
                                        OVER (
                                            PARTITION BY item_name
                                            ORDER BY new_start_date, new_end_date
                                        )
                                        prevend,
                                    LEAD(new_start_date)
                                        OVER (
                                            PARTITION BY item_name
                                            ORDER BY new_start_date, new_end_date
                                        )
                                        nextstart
                               FROM sample_table
                              WHERE item_name = 'SONY')
                      WHERE new_start_date < prevend AND new_end_date > nextstart);
 
DELETE FROM sample_table
      WHERE ROWID IN
                    (SELECT rid
                       FROM (SELECT ROWID rid,
                                    new_start_date,
                                    new_end_date,                                   
                                    LAG(new_end_date)
                                        OVER (
                                            PARTITION BY item_name
                                            ORDER BY new_start_date, new_end_date
                                        )
                                        prevend
                               FROM sample_table
                              WHERE item_name = 'SONY')
                      WHERE new_start_date < prevend );

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:

Select allOpen in new window

 

by: Sharath_123Posted on 2009-03-31 at 15:43:29ID: 24034548

<<I don't understand the strategy behind the query,
I do understand the functionality of it, just not the why. >>

Do you want explanation?

select t1.MODEL,t1.PRICE,t1.START_DATE_ACTIVE,t1.END_DATE_ACTIVE
  from (select *,ROW_NUMBER() over (partition by Model order by Price) RN from your_table) t1
  left join (select *,ROW_NUMBER() over (partition by Model order by Price) RN from your_table) t2
    on t1.Model = t2.Model and t1.RN - 1 = t2.RN
  left join (select *,ROW_NUMBER() over (partition by Model order by Price) RN from your_table ) t3
    on t1.Model = t3.Model and t1.RN + 1 = t3.RN
 where case when (t1.START_DATE_ACTIVE < t2.END_DATE_ACTIVE) and 
                 (t1.END_DATE_ACTIVE > t3.START_DATE_ACTIVE) and 
                 (t1.END_DATE_ACTIVE < t3.END_DATE_ACTIVE)  then 1 else 0 end = 0
 order by t1.Model,t1.RN
/* query result
MODEL	PRICE	START_DATE_ACTIVE	END_DATE_ACTIVE
SONY	100	1901-01-01 00:00:00.000	2005-12-31 00:00:00.000
SONY	200	2006-01-01 00:00:00.000	2007-12-31 00:00:00.000
SONY	300	2008-01-01 00:00:00.000	2008-12-31 00:00:00.000
SONY	500	2009-01-01 00:00:00.000	2010-12-31 00:00:00.000
*/

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:

Select allOpen in new window

 

by: sdstuberPosted on 2009-03-31 at 17:22:38ID: 24035030

not an explanation of the functionality (results) no.  I understand what it does and how it works.

but what are you trying to do?  it clearly produces different results than what the asker is looking for.
I assume you are doing something different intentionally, so what is it/why that approach?

Particularly since it's so "heavy" with 3 outer joins.

So, I know what it does, and even if I didn't, I ran it myself (after fixing small syntax errors) and saw the results.
I'm just not sure what direction you're going.

It might be a good approach but "as is" I don't know how to use what you've posted.

 

by: Sharath_123Posted on 2009-03-31 at 18:36:07ID: 24035374

>> it clearly produces different results than what the asker is looking for.

Is it? Can you provide some example set where you are getting different results.

Asker - can you test this on your whole sample data and let me know where its failing. I don' say that its the perfect solution, i just tested this with the sample set provided but check against your complete table.

>> Particularly since it's so "heavy" with 3 outer joins.

JOINing 3 times may be over head, but to achieve the required output, i used this approach.

>> I assume you are doing something different intentionally, so what is it/why that approach?

I don't understand your meaning of "intentionally" here. I am trying to achieve the asker's expected result set.  

To remove the overlapping records, i am checking previous record and next records Start /End dates with current record's Start/End dates. (The condition in my CASE statement).

 

by: sdstuberPosted on 2009-03-31 at 18:53:41ID: 24035445

Try this...

I've modified your query to fit the most recent structure posted by the asker
I populated it with the data from the spreadsheet, but changed the first row to not go back to 1901 then added more historical data


the goal is to delete the rows with overlaps
with the sample data below there are 2 overlaps
the row with a price of 400 and the second row with a price of 17500

If you run the 2 deletes I posted earlier you'll be left with 10 non-overlapping rows.

Your query doesn't return the 2 rows that should be deleted

I tried looking at in reverse, maybe you were trying to return the data that would be left after deletes; but it doesn't do that either.  

That's what I meant by "doing something intentionally".  It didn't appear to be a small mistake pursuing the same goal I had ben and simply got some unexpected results.  Your approach appeared so radically different from where the thread had been going that it seemed like you must be doing something different and on purpose and I simply don't understand.

drop table sample_table;
 
CREATE TABLE sample_table
AS
    SELECT price_list_name,
           item_name,
           price,
           TO_DATE(nsd, 'yyyy/mm/dd') new_start_date,
           TO_DATE(ned, 'yyyy/mm/dd') new_end_date
      FROM (SELECT 'Global Price List All' price_list_name,
                   'SONY' item_name,
                   15500 price,
                   '2005/01/01' nsd,
                   '2005/05/14' ned
              FROM DUAL
            UNION ALL
            SELECT 'Global Price List All', 'SONY', 16000, '2005/05/15', '2006/02/04' FROM DUAL
            UNION ALL
            SELECT 'Global Price List All', 'SONY', 16800, '2006/02/05', '2007/06/09' FROM DUAL
            UNION ALL
            SELECT 'Global Price List All', 'SONY', 17000, '2007/06/10', '2007/12/31' FROM DUAL
            UNION ALL
            SELECT 'Global Price List All', 'SONY', 17500, '2008/01/01', '2008/12/31' FROM DUAL
            UNION ALL
            SELECT 'Global Price List All', 'SONY', 17500, '2008/12/01', '2008/12/31' FROM DUAL
            UNION ALL
            SELECT 'Global Price List All', 'SONY', 18000, '2009/01/01', '2010/12/31' FROM DUAL
            union all
            SELECT 'Global Price List All', 'SONY', 100, '1901/01/01', '1995/12/31' FROM DUAL
            UNION ALL
            SELECT 'Global Price List All', 'SONY', 200, '1996/01/01', '1997/12/31' FROM DUAL
            UNION ALL
            SELECT 'Global Price List All', 'SONY', 300, '1998/01/01', '1998/12/31' FROM DUAL
            UNION ALL
            SELECT 'Global Price List All', 'SONY', 400, '1998/02/01', '1999/12/31' FROM DUAL
            UNION ALL
            SELECT 'Global Price List All', 'SONY', 500, '1999/01/01', '2000/12/31' FROM DUAL);
            
select t1.item_name,t1.PRICE,t1.new_START_DATE,t1.new_END_DATE
  from (select sample_table.*,ROW_NUMBER() over (partition by item_name order by Price) RN from sample_table) t1
  left join (select sample_table.*,ROW_NUMBER() over (partition by item_name order by Price) RN from sample_table) t2
    on t1.item_name = t2.item_name and t1.RN - 1 = t2.RN
  left join (select sample_table.*,ROW_NUMBER() over (partition by item_name order by Price) RN from sample_table ) t3
    on t1.item_name = t3.item_name and t1.RN + 1 = t3.RN
 where case when (t1.new_START_DATE < t2.new_END_DATE) and 
                 (t1.new_END_DATE > t3.new_START_DATE) and 
                 (t1.new_END_DATE < t3.new_END_DATE)  then 1 else 0 end = 0
 order by t1.item_name,t1.RN            

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:

Select allOpen in new window

 

by: Sharath_123Posted on 2009-03-31 at 19:22:45ID: 24035549

I missed one more condition in my previous post.

yes, I am trying to return the data that would be left after deletes.

select t1.item_name,t1.PRICE,t1.new_START_DATE,t1.new_END_DATE
  from (select sample_table.*,ROW_NUMBER() over (partition by item_name order by Price) RN from sample_table) t1
  left join (select sample_table.*,ROW_NUMBER() over (partition by item_name order by Price) RN from sample_table) t2
    on t1.item_name = t2.item_name and t1.RN - 1 = t2.RN
  left join (select sample_table.*,ROW_NUMBER() over (partition by item_name order by Price) RN from sample_table ) t3
    on t1.item_name = t3.item_name and t1.RN + 1 = t3.RN
 where case when t1.new_START_DATE <= t2.new_END_DATE 
            and ((t1.new_END_DATE <= t2.new_END_DATE) or 
                (t1.new_END_DATE >= t2.new_END_DATE and t1.new_END_DATE >= t3.new_START_DATE)) then 1 else 0 end = 0
 order by t1.item_name,t1.RN 

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:

Select allOpen in new window

 

by: sdstuberPosted on 2009-03-31 at 20:45:58ID: 24035816

ok, maybe I'm misunderstanding the request

I thought the goal was to actually "do" the deletes of the invalid rows

Suriyaraj_Sudalaiappan, can you please clarify?

 

by: sdstuberPosted on 2009-03-31 at 20:55:12ID: 24035849

If the goal is simply to return the good data
and not actually delete the bad stuff
then try this...

SELECT price_list_name, item_name, price, new_start_date, new_end_date
  FROM (SELECT price_list_name,
               item_name,
               price,
               new_start_date,
               new_end_date,
               LAG(new_end_date)
                   OVER (PARTITION BY item_name ORDER BY new_start_date, new_end_date)
                   prevend
          FROM (SELECT price_list_name,
                       item_name,
                       price,
                       new_start_date,
                       new_end_date,
                       LAG(new_end_date)
                           OVER (PARTITION BY item_name ORDER BY new_start_date, new_end_date)
                           prevend,
                       LEAD(new_start_date)
                           OVER (PARTITION BY item_name ORDER BY new_start_date, new_end_date)
                           nextstart
                  FROM sample_table
                 WHERE item_name = 'SONY')
         WHERE new_start_date > NVL(prevend, TO_DATE('1-1-1', 'yyyy-mm-dd'))
            OR new_end_date < NVL(nextstart, TO_DATE('9999-12-31', 'yyyy-mm-dd')))
 WHERE new_start_date > NVL(prevend, TO_DATE('1-1-1', 'yyyy-mm-dd'))

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:

Select allOpen in new window

 

by: Suriyaraj_SudalaiappanPosted on 2009-04-01 at 23:51:20ID: 24047088

Hi,
  Shall i use the query like below. In my table, i have 9000 rows. I did not give any where condition to select the particular model.

SELECT rid
FROM (SELECT ROWID rid,
            price_list_name,
            item_name,
            uom_code,
            new_start_date,
            new_end_date,
            formula_name,
            LAG(new_end_date)
                OVER (
                    PARTITION BY item_name
                    ORDER BY new_start_date, new_end_date
                )
                prevend,
            LEAD(new_start_date)
                OVER (
                    PARTITION BY item_name
                    ORDER BY new_start_date, new_end_date
                )
                nextstart
        FROM SAMPLE_TABLE)
        WHERE (new_start_date < prevend)
        OR (new_start_date < prevend AND new_end_date > nextstart);

 

by: sdstuberPosted on 2009-04-02 at 04:53:11ID: 24048653

what do you want to do with that query?


as you can see from the threads above there is some confusion as to what you are really trying to accomplish.
I have two guesses as to your goal and I've posted answers to both of them.


if you want to do a delete of data from your table, then no
use the two deletes I posted before in 24034314, run them in that same order

if you want to show records that will be kept if you were to do a delete then use the query I posted in 24035849 (you could also use Sharath's but it's less efficient)


if neither of those goals is really what you're trying to do, please try explaining again.

 

by: sdstuberPosted on 2009-04-02 at 04:53:48ID: 24048660

you can remove the model or item_name condition, of course

 

by: Suriyaraj_SudalaiappanPosted on 2009-04-02 at 05:42:22ID: 24049022

essdbdu33 $cp l3109359.dbg $XXOM_TOP/log
essdbdu33 $ls -l $XXOM_TOP/log
total 800
-rw-r--r--   1 rhuggi   aosimp    401791 Apr  1 12:53 l3109359.dbg
essdbdu33 $>l3109359.dbg
bash: l3109359.dbg: Permission denied
essdbdu33 $pwd
/apps/betsyp1/betsyp1db/10.2.0/temp/betsyp1_essdbdu33

Hi,
Actually i want to update my custom table if any overlapping data are there.
        Actually my requirement is, we need to identify the overlapping data from the table and update the same. Here overlapping means, we have to consider only the start date and end date columns. We have 'n' number of items in my table and it contains different prices. Price is different for same item with different start and end date. That means, price is applicable for only with in that period. So we should be start from one date and end with other date. That date should not be repeated multiple times. For example my sample data, it start with 1/1/2000 and end with 12/31/2001. Then the next date should start with 1/1/2002. It should not start 1/2/2002 and more than that. because my data is like that. If any start like 1/2/2002 and more than that we should update that row in the table as overlapping.
      And i got two scenarios from my data sheet like below. The start date is same for two rows and end date is different. Some data start date is different and end date is same. I will give the example. In this we don't know which one we should remove. For that discussion is going on right now. I will let you know after they confirmed.

For first scenario:
START_DATE      END_DATE
10/28/2006      12/31/2006
10/28/2006      12/31/2010
For second scenario:
START_DATE      END_DATE
1/1/2008            12/31/2010
1/2/2008            12/31/2010

 Below query i modified from your query. Please verify that and let me know if any changes. And i included one more condition here like pps1.new_end_date <> prevend. It will helpful for my second scenario to remove same end date and consider only one. But not decided this one.
UPDATE /*+index(pps) */
                XXQP_PAM_PRICE_STG pps
            SET
                pps.process_flag = 3,
                pps.error_msg = 'Overlapping data, not consider for Import'
            WHERE
                pps.process_flag = 1
                AND pps.ROWID IN (
                            SELECT /*+index(pps1) */ rid
                            FROM (
                                SELECT ROWID rid,
                                        price_list_name,
                                        item_name,
                                        uom_code,
                                        new_start_date,
                                        new_end_date,
                                        formula_name,
                                        LAG(new_end_date)
                                            OVER (
                                                PARTITION BY item_name
                                                ORDER BY new_start_date, new_end_date
                                            )
                                            prevend,
                                        LEAD(new_start_date)
                                            OVER (
                                                PARTITION BY item_name
                                                ORDER BY new_start_date, new_end_date
                                            )
                                            nextstart
                                    FROM XXQP_PAM_PRICE_STG
                                    ) pps1
                            WHERE ((pps1.new_start_date < prevend AND pps1.new_end_date <> prevend) OR (pps1.new_start_date < prevend AND pps1.new_end_date > nextstart))
                            AND pps1.item_name = pps.item_name
                            );

Thanks      

 

by: Suriyaraj_SudalaiappanPosted on 2009-04-02 at 05:43:39ID: 24049034

don't consider from the staring in my previous comment. some copy pase mistake. Please read from "HI"

 

by: sdstuberPosted on 2009-04-02 at 06:10:45ID: 24049321

so,  this entire question has been wrong then it seems.

you're not trying to do a delete at all
and, you're not just trying to find overlaps, but you're also trying to find holes?

your description of one record ending 12/31/2001 but the next record should not start 1/2/2002
is that because it would leave a "hole" of 1/1/2001?


please post complete example of data (not a query)
please use a create table like I posted above with your entire table structure and enough sample data to show all scenarios (good and bad) you want to consider.

then post what you want the table to look like afterward.  Don't worry about trying to guess what query might be used, just manually create the data in its final form and we'll try to figure out what query will produce the results you are looking for






 

by: sdstuberPosted on 2009-04-02 at 08:22:01ID: 24050832

by the way, please close your old questions, you have open questions, including apparent repeats (I just posted to one of them) over 2 months old.

 

by: Suriyaraj_SudalaiappanPosted on 2009-07-03 at 10:28:52ID: 31564275

GOOD ONE

 

by: sdstuberPosted on 2009-07-03 at 17:06:00ID: 24775005

glad I could help

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...