How do I identify records in table where dates overlap?

Hi
In a table I can have multiple records per Code. But if you look at the Start and End dates of the last two records they overlap.
Unique ID Code Start Date End Date
1 143     28-MAY-02     16-OCT-02     
2 143     17-OCT-02      22-NOV-02     
3 143     13-NOV-02     12-MAR-03
I want to be able to identify these and then change the values to be     
Unique ID Code Start Date End Date
1 143     28-MAY-02     16-OCT-02     
2 143     17-OCT-02      12-NOV-02     
3 143     13-NOV-02     12-MAR-03
i.e. set the End Date of the 2nd record to be a day less than the start date of the 3rd record.
Can anyone please help with how I can do this?
Thanks
GB

You have to make the ordering unique, so I added the column end_date to the ordering.
And the solution prevented was buggy, so I replaced it with a new SQL statement:
SQL> create table test_table
  2  as
  3  select 1 uqid, 143 code, date '2002-05-28' start_date, date '2002-10-16' end_date from dual union all
  4  select 2, 143, date '2002-10-17', date '2002-11-22' from dual union all
  5  select 3, 143, date '2002-11-13', date '2003-03-12' from dual union all
  6  select 1, 100, date '2007-02-01', date '2007-02-14' from dual union all
  7  select 2, 100, date '2007-02-15', date '2007-02-15' from dual union all
  8  select 3, 100, date '2007-02-15', date '2007-05-05' from dual
  9  /
Tabel is aangemaakt.
SQL> select uqid
  2       , code
  3       , start_date
  4       , end_date
  5       , next_date -1 as replacement_date
  6    from ( select uqid
  7                , code
  8                , start_date
  9                , end_date
10                , LAG(start_date) over (partition by code order by start_date desc) as next_date
11             from test_table
12         )
13   order by code
14       , start_date
15  /
UQID  CODE START_DATE          END_DATE            REPLACEMENT_DATE
    1   100 01-02-2007 00:00:00 14-02-2007 00:00:00 14-02-2007 00:00:00
    2   100 15-02-2007 00:00:00 15-02-2007 00:00:00
    3   100 15-02-2007 00:00:00 05-05-2007 00:00:00 14-02-2007 00:00:00
    1   143 28-05-2002 00:00:00 16-10-2002 00:00:00 16-10-2002 00:00:00
    2   143 17-10-2002 00:00:00 22-11-2002 00:00:00 12-11-2002 00:00:00
    3   143 13-11-2002 00:00:00 12-03-2003 00:00:00
6 rijen zijn geselecteerd.
SQL> select uqid
  2       , code
  3       , start_date
  4       , greatest
  5         ( least
  6           ( lead(start_date,1,end_date+1) over (partition by code order by start_date, end_date) - 1
  7           , end_date
  8           )
  9         , start_date
10         ) replacement_date
11    from test_table
12   order by code
13       , start_date
14       , end_date
15  /
UQID  CODE START_DATE          REPLACEMENT_DATE
    1   100 01-02-2007 00:00:00 14-02-2007 00:00:00
    2   100 15-02-2007 00:00:00 15-02-2007 00:00:00
    3   100 15-02-2007 00:00:00 05-05-2007 00:00:00
    1   143 28-05-2002 00:00:00 16-10-2002 00:00:00
    2   143 17-10-2002 00:00:00 12-11-2002 00:00:00
    3   143 13-11-2002 00:00:00 12-03-2003 00:00:00
6 rijen zijn geselecteerd.Regards,
Rob.

Similar Messages

  • How do you delete records from table with data in a select option

    how do you delete records from table with relevant to data in a select option..how to write coding

    Hi,
    Try
    if not s_select_option [ ] is initial.
    delete * from table
    where field in s_select_option.
    endif.
    commit work.
    Be careful though. If select option is emty, you will delete the entire table.
    Regards,
    Arek

  • How to create a record for table PLAF with order type 'NB'.

    How to create a record for table PLAF with order type 'NB'(standard purchase order).
    who can tell me the T-code or some usefull information?
    Thanks.

    Hi
    This will be updated automatically when generate planned orders thru MRP. (MD02)
    regards
    Srinivas

  • Asset related question - How to add a record into table ANLC?

    Hello experts,
    Could anyone tell me how to add a record into table ANLC?
    Thanks very much!
    Christina.

    how you want add?
    you want add direct in table???.
    Normally if yo post any transaction this table will update.
    ex;acquisition;
    chandra

  • How to compare two records of table

    Hi all,
    How to compare new record with all existing records in table.
    In my application i have status column ,which contains the information about
    the record is new or old record .
    thank you

    /*does record 1 have friend*/
    WITH t AS
    (SELECT 1 AS Id, 'a' AS NAME, 'type1' AS Col
      FROM Dual
      UNION ALL
      SELECT 2 AS Id, 'a' AS NAME, 'type1' AS Col
      FROM Dual
      UNION ALL
      SELECT 3 AS Id, 'a' AS NAME, 'type2' AS Col FROM Dual)
    SELECT count(*) as YesNo
    FROM t T1
    WHERE T1.Id = 1
          AND (T1.Name, T1.Col) IN (SELECT T2.Name, T2.Col FROM T t2 where t2.ID != T1.Id);
         1
    /*does record 3 have friend*/
    WITH t AS
    (SELECT 1 AS Id, 'a' AS NAME, 'type1' AS Col
      FROM Dual
      UNION ALL
      SELECT 2 AS Id, 'a' AS NAME, 'type1' AS Col
      FROM Dual
      UNION ALL
      SELECT 3 AS Id, 'a' AS NAME, 'type2' AS Col FROM Dual)
    SELECT count(*) as YesNo
    FROM t T1
    WHERE T1.Id = 3
       AND (T1.Name, T1.Col) IN (SELECT T2.Name, T2.Col FROM T t2 where t2.ID != T1.Id);
         0
    */

  • How to Display multiple records in Table in VC without using BAPI.

    Hi All,
    I am working on Visual composer (NW2004s SP10). I am trying to display Poitems from BAPI_PO_GETDETAIL. I am creating my front end using VC. I have created one form and one Table where I want to display POItems. I am writing my logic of retrieving data from BAPI in CAF.I am connecting them in Guided procedures.When I run my process in Guided Procedures I am getting single row displayed in table. Can Anyone help me how to display multiple rows in table.
    Regards,
    Sheetal

    Hi Sheetal,
    if the BAPI returns a table, then you get multiple rows. From which system is the BAPI, so that I can check the BAPI to give you further information.
    Best Regards,
    marcel

  • How to achieve that "SELECT * FROM table WHERE age BETWEEN 18 AND 23 AND n"

    How to achieve the SQL like that "SELECT * FROM table WHERE age BETWEEN 18 AND 23 AND name = 'Will' " with BDB C-API
    The primary key in the primary database is 'age' ,and the secondary key in the secondary index is 'name' ,in a lot of examples ,there are all simple , but how to do that complex one.thx~

    but this means that the prepared statement is created
    each time I call my method and so I'm not sure that
    the optimizer will find it easy to cope with.You are right, the optimizer won't find that easy to deal with (presuming that is even relevant for your driver/database.) But most optimizers won't do anything with statements that change and that is what you are doing.
    You could create several prepared statements which have a common number of bind variables. For example 10 statements with from 1 to 10 bind values. This will work if most of the queries use those.

  • How do I do SELECT * FROM TABLE WHERE KEY IN ({list})?

    The title says it all really.
    Is there a reasonable performant way to perform the query
    SELECT * FROM TABLE WHERE KEY IN ({list})where {list} is String []?
    I am currently creating a PreparedStatement with a for loop like this   StringBuffer sb = new StringBuffer ("SELECT * FROM TABLE WHERE ID IN (");
      for (int ii=0;ii<keys.length;ii++) {
        sb.append (keys [ii]);
        if (ii != keys.length-1) sb.append (",");
      sb.append (")");but this means that the prepared statement is created each time I call my method and so I'm not sure that the optimizer will find it easy to cope with. Is there a construction that I'm missing along the lines of SELECT * FROM TABLE WHERE KEY = ? where I can create the PreparedStatement once and just call setObject or something on it when the values in {list} change?

    but this means that the prepared statement is created
    each time I call my method and so I'm not sure that
    the optimizer will find it easy to cope with.You are right, the optimizer won't find that easy to deal with (presuming that is even relevant for your driver/database.) But most optimizers won't do anything with statements that change and that is what you are doing.
    You could create several prepared statements which have a common number of bind variables. For example 10 statements with from 1 to 10 bind values. This will work if most of the queries use those.

  • How do I make form or table where certain numbers update according to a formula when the user enters one number?

    Hi, I am an amature website designer and have made a website
    using dreamweaver. I am very lost when it comes to doing tougher
    things in dreamweaver. I want to create a small form or a table
    where there will be 7 or so numbers that are generated based on
    simple formulas for each one once you enter one number. Can someone
    please help me with this task whether it is in dreamweaver or even
    making it out of flash or anyway that this would be the easiest to
    make possible. Thanks

    Here's a very simple form you could use to perform a
    calculation, it this case, squares the input value and writes it
    back to another input field... Hopefully you can use this as a
    pattern to apply your own algorithm. You'll need to do validation
    to assure the entered fields are appropriate for the calculation
    (required fields are entered).
    Hope this gets you started.
    ================================================
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
    Transitional//EN" "
    http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="
    http://www.w3.org/1999/xhtml">
    <head>
    <meta content="text/html; charset=utf-8"
    http-equiv="Content-Type" />
    <script type="text/javascript">
    function squareit() {
    var num = document.getElementById('myinput');
    var res = document.getElementById('myresults');
    res.value = Math.pow(parseFloat(num.value), 2);
    </script>
    </head>
    <body>
    <form id="myform">
    <label for="myinput">Number: </label><input
    id="myinput"><br/>
    <label for="myresults">Result: </label><input
    id="myresults"><br/>
    <input type="button" value="Calculate"
    onclick="squareit()"/>
    </form>
    </body>
    </html>

  • How can I reference records outside the two date parameters?

    Hi all,
    I have a query that fetches records based on the two date parameters defined (Startdate and Enddate).
    If the Startdate is 2014-12-01 and the Enddate is 2014-12-12, I want to pull records outside these two date parameters, that is      2014-09-01 and 2014-11-30.
    I want to add up the records from  2014-09-01 and 2014-11-30 and include them in one of the columns in my report.
    I tried using this query:
     SUM(CASE WHEN FilteredIncident.Statuscodename IN ('QUEUED', 'ASSIGNED') AND (EnteredOn >= '2014-09-01' AND EnteredOn<= @StartDate) THEN 1 ELSE 0 END) AS OpenRecords
    Please help with any ideas..thanks

    Please follow basic Netiquette and post the DDL we need to answer this. Follow industry and ANSI/ISO standards in your data. You should follow ISO-11179 rules for naming data elements. You should follow ISO-8601 rules for displaying temporal data. We need
    to know the data types, keys and constraints on the table. Avoid dialect in favor of ANSI/ISO Standard SQL. And you probably need to read and download the PDF for: 
    https://www.simple-talk.com/books/sql-books/119-sql-code-smells/
    There is no such crap as a “status_code_name” in RDBMS. It has to be a “<something in particular>_status”; think about how silly that data element name is! Want to keep going and have a “status_code_name_value_id”? LOL! 
    The name “Filtered_Incident” is also wrong. Tables are sets, so unless you have only one element in this set, the table name should be a plural or (better) collective name. But a better question is why  did you split out “Filtered_Incidents” from “Incidents”?
    Would you also split “Male_Personnel” and “Male_Personnel” from “Personnel”? 
    Get a book on data modeling and learn some basics. 
    >> I have a query that fetches records [sic: rows are nor records] based on the two date parameters defined (report_start_date and report_end_date). If the report_start_date is 2014-12-01 and the report_end_date is 2014-12-12, I want to pull records [sic]
    outside these two date parameters, that is 2014-09-01 and 2014-11-30. I want to add up the records [sic] from 2014-09-01 and 2014-11-30 and include them in one of the columns in my report. <<
    Having no DDL and no sample data makes this hard. Does your boss make you program without any documentation, DDL, etc? This spec is vague; you say to do a total, but show a count, etc. 
    One of the many nice things about DATE data types is that the BETWEEN predicate works with them, so you can quite writing 1960's BASIC predicates with primitive logic operators. 
    Here is a guess: 
    SELECT SUM(CASE WHEN incident_date BETWEEN '2014-09-01' 
               AND @report_start_date THEN 1 ELSE 0 END)
           AS open_record_cnt 
      FROM Incidents
     WHERE incident_status IN ('QUEUED', 'ASSIGNED')
        AND incident_date <= @report_end_date; 
    --CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
    in Sets / Trees and Hierarchies in SQL

  • How can i compare 2 internal table's data which have  same structure ?

    hi friends,
    i want to know how  to compare 2 internal table's data which have  same structure

    DATA: BEGIN OF LINE,
    COL1 TYPE I,
    COL2 TYPE I,
    END OF LINE.
    DATA: ITAB LIKE TABLE OF LINE,
    JTAB LIKE TABLE OF LINE.
    DO 3 TIMES.
    LINE-COL1 = SY-INDEX.
    LINE-COL2 = SY-INDEX ** 2.
      APPEND LINE TO ITAB.
    ENDDO.
    MOVE ITAB TO JTAB.
    LINE-COL1 = 10. LINE-COL2 = 20.
    APPEND LINE TO ITAB.
    IF ITAB GT JTAB.
    WRITE / 'ITAB GT JTAB'.
    ENDIF.
    APPEND LINE TO JTAB.
    IF ITAB EQ JTAB.
    WRITE / 'ITAB EQ JTAB'.
    ENDIF.
    LINE-COL1 = 30. LINE-COL2 = 80.
    APPEND LINE TO ITAB.
    IF JTAB LE ITAB.
    WRITE / 'JTAB LE ITAB'.
    ENDIF.
    LINE-COL1 = 50. LINE-COL2 = 60.
    APPEND LINE TO JTAB.
    IF ITAB NE JTAB.
    WRITE / 'ITAB NE JTAB'.
    ENDIF.
    IF ITAB LT JTAB.
    WRITE / 'ITAB LT JTAB'.
    ENDIF.
    The output is:
    ITAB GT JTAB
    ITAB EQ JTAB
    JTAB LE ITAB
    ITAB NE JTAB
    ITAB LT JTAB
    This example creates two standard tables, ITAB and JTAB. ITAB is filled with 3 lines and copied to JTAB. Then, another line is appended to ITAB and the first logical expression tests whether ITAB is greater than JTAB. After appending the same line to JTAB, the second logical expression tests whether both tables are equal. Then, another line is appended to ITAB and the third logical expressions tests whether JTAB is less than or equal to ITAB. Next, another line is appended to JTAB. Its contents are unequal to the contents of the last line of ITAB. The next logical expressions test whether ITAB is not equal to JTAB. The first table field whose contents are different in ITAB and JTAB is COL1 in the last line of the table: 30 in ITAB and 50 in JTAB. Therefore, in the last logical expression, ITAB is less than JTAB.
    regards,
    srinivas
    <b>*reward for useful answers*</b>

  • Can please tell me how to implement expand and collapse table row data?

    i am trying implement expand and collapse table row data but i do not get any ideas..can please any one help me its an urgent requirement

    Yes, we can.   
    I think the best place for you to start for this is the NI Developer Zone.  I recommend beginning with these tutorials I found by searching on "data log rio".  There were more than just these few that might be relevant to your project but I'll leave that for you to decide.
    NI Compact RIO Setup and Services ->  http://zone.ni.com/devzone/cda/tut/p/id/11394
    Getting Started with CompactRIO - Logging Data to Disk  ->  http://zone.ni.com/devzone/cda/tut/p/id/11198
    Getting Started with CompactRIO - Performing Basic Control ->  http://zone.ni.com/devzone/cda/tut/p/id/11197
    These will probably give you links to more topics/tutorials/examples that can help you design and implement your target system.
    Jason
    Wire Warrior
    Behold the power of LabVIEW as my army of Roomba minions streaks across the floor!

  • How to hide a record in table from users of different sessions ?

    Hi,
    I am having a table say 'EMPLOYEE' with 10 records in it and users X and Y.
    X fetched a row from his session, assume 5th record for perfoming update operation .
    Until X commits his transaction , this 5th record should not viewable to user Y evenwith
    "SELECT * FROM EMPLOYEE" statement .
    How it is possible with oracle ?
    Thanx in advance.
    Regards,
    Hariharan ST

    Look at this example please
    SQL> create user test1 identified by test1;
    User created.
    SQL> grant dba to test1;
    Grant succeeded.
    SQL> conn test1/test1
    Connected.
    SQL> create table test1_id (id number);
    Table created.
    SQL> begin
      2  for i in 1..5 loop
      3  insert into test1_id values(i);
      4  end loop;
      5  commit;
      6  end;
      7  /
    PL/SQL procedure successfully completed.
    SQL> select * from test1_id;
            ID
             1
             2
             3
             4
             5
    SQL> disc
    Disconnected from Oracle Database 10g Enterprise Edition Release 10.1.0.5.0 - Production
    With the Partitioning, OLAP and Data Mining options
    SQL> conn / as sysdba
    Connected.
    SQL> create user x identified by x;
    User created.
    SQL> grant dba to x;
    Grant succeeded.
    SQL> create user y identified by y;
    User created.
    SQL> grant dba to y;
    Grant succeeded.
    SQL> conn x/x
    Connected.
    SQL> update test1.test1_id set id=1;
    5 rows updated.
    SQL>
    And now connected by X user, from another session I'm connecting with Y user and issue:
    SQL> conn y/y
    Connected.
    SQL> select * from test1.test1_id;
            ID
             1
             2
             3
             4
             5
    SQL>- - - - - - - - - - - - - - - - - - - - -
    Kamran Agayev A. (10g OCP)
    http://kamranagayev.wordpress.com

  • 10g How to Refresh / Reload records in table

    I am extremely new to JDeveloper and I am just trying to get my head around a few of the more simple aspects. I have worked through a few tutorials and learnt the basics.
    I have only one table involved as what I am trying to achieve is quite simple. I have a browse form (where I can see all records in the table and navigate my way through them), an edit form (where I can edit a record using an edit link associated with the table row) and a find form (where I can type in details to pull matching records). I have linked the find results form to the edit page so that once a record is found the user can edit this record. This bit works fine (surprised the hell out of me) but the problem is that when the edits are made and the confirm button is clicked the screen returns to the browse page (this is correct) but in the browse table there is only the record I just edited, I can't see all the others. I'm pretty sure there must be an easy way to fix this with some kind of refresh/reload data action in between the find/edit and browse page. Please can someone help.
    If you need any further information let me know, I hope this simple query can be solved easily. Many thanks, Ben Sayers

    Frank,
    I used the basic find mode for this. Executing the query finds the records that match but when I have finished with the 'finding' and just want to browse all records again, what code do I need and where do I put it?
    Thanks you for you help, once I've got my head around this I should be able to fumble my way through the rest. But all the tutorials so far seem so high-level and I am so far away from the code I have no idea how to actually implement anything myself. If you could detail the sets for this I would be able to start to understand the workings of things.
    Many thanks,
    Ben Sayers

  • How to delete the record in Table

    Hi Guru's,
    i have Table which contain no.of Records.
    i want to deleted one record. if i go to Table maint.Generator....from table itself..
    how to do that... when we deleting the record. can we create new TR for that?
    can anybody tell me.
    Thanks in Advance,
    venkat

    Hi,
    it is answered, here for my table there is no Table Maint.Generator.
    i just explained how i have done it.
    i just simply gone into Debug mode. there
    code = Dele.
    i have given. then i came out from debug mode to Table. there i just got Delete button on application tool bar.
    i selected the record then icliked on Delete button.
    it is got deleted.
    But it is not asking for any new Transport Request.
    Regards,
    Venkat

Maybe you are looking for

  • Forms are not coming up

    Hello All, In our 11.5.10.2, sometimes, Forms are not coming up with no error messages populated; Form launcher is not even launched. Again, this is happening from time to time, with different users and different Forms. What are the stuff that I need

  • How to print unicode in SAPscript

    hi gurus, I am now having a problem in printing uinicode in printing ( printer HP LaserJet 4250 PCL 6) with SAPscript. Although i have changed the font in SAPscript text ( paragraph format ) , but when i print out , the font does not change , coul yo

  • IPhoto missing events

    I am using OS X Lion and the latest version of iPhoto.  Last night I imported about 100 images and created an event.  I then synched my iPhone and all the images were on the phone. All was well. Today I again importeed a couple of images and I notice

  • Safari failing on pages where Firefox doesn't

    Hi I am having this weird problem. I thought for a while that my internet was playing up when some pages, but not others, would open properly. The bad pages would just give the error "Safari can't find the server". But then I discovered the same page

  • Bank Details Infotype 9

    Is it possible to limit (i.e. set a maximum) number of direct deposit banks for employees?  For instance, we would like to limit the number of banks employees can send direct deposits to at 3 per employee.  Thanks!