Find the gap and overlap

Hi gurus

I have the following data:

Data

SELECT rule_id 100, Min_age 18, Max_age 34, 'SINGLE' status, 1 double bill_id

UNION ALL

SELECT rule_id 100, Min_age 18, Max_age 34, the status of 'FAMILY', 3 bill_id FROM dual

UNION ALL

SELECT rule_id 100, Min_age 18, Max_age 34, 'COUPLE' status, 5 double bill_id

UNION ALL

SELECT rule_id 100, Min_age 35, 44 Max_age, 'COUPLE' status, 8 double bill_id

UNION ALL

SELECT rule_id 100, Min_age 18, Max_age 34, 'COUPLE' status, 9 double bill_id

UNION ALL

SELECT rule_id 100, Min_age 20, Max_age 25, 'COUPLE' status, 10 double bill_id

UNION ALL

SELECT rule_id 100, Min_age 35, 44 Max_age, "SINGLE" status, 11 double bill_id

UNION ALL

SELECT rule_id 100, Min_age 46, 50 Max_age, 'COUPLE' status, 12 bill_id FROM dual;

------------------

The query result

rule_id Min_age Max_age status bill_id

100                     18                     34                 SINGLE           1

100                     18                     34                 FAMILY           3

100                     18                     34                 COUPLE         5

100                     35                     44                COUPLE          8

100                     18                     34                COUPLE          9

100                     20                     25                COUPLE         10

100                     35                     44                SINGLE           11

100                     46                     50                COUPLE         12

Power required

100                     18                     34                COUPLE          9

100                     20                     25                COUPLE         10

100                     46                     50                COUPLE         12

Rules

1. record may be overlapping, but same Min_age, Mix_age and rule_id even with records of different status, for example the first 3... (bill_id 1,3,5) but if you can look in the folder bill_id = 9 status couple is repeated against the same Min_age, Mix_age and rule_id even if I need to get this record...

2 - the need to fetch the record of overlap as bill_id = 10 because its between 18 and 34.

3 day-1 gap is ok if you see bill_id = 5 max_age = 34 and next Min_age = 35 so this record is ok, but if you look in bill_id = 11 his max_age = 44 then next Min_age = 46 ways to the gap... so I need this file too

I try my level best to convey my message, but if you have any questions please let me know.

Concerning

Shu

Something like:

Select *.

of tbl t1

When there is)

Select 1

of tbl t2

where t2.status = t1.status

and)

T1.MIN_AGE between t2.min_age and t2.max_age

or

T1.MAX_AGE between t2.min_age and t2.max_age

)

and t2.bill_id<>

)

or min_age! = ()

Select nvl (max (t2.max_age) + 1, t1.min_age)

of tbl t2

where t2.status = t1.status

and t2.max_age<>

)

/

RULE_ID MIN_AGE MAX_AGE STATUS BILL_ID
---------- ---------- ---------- ------ ----------
100 18 34 COUPLE 9
100 20 25 10 COUPLE
100 46 50 TORQUE 12

Scott@ORCL >

SY.

Tags: Database

Similar Questions

  • Calculations of dates for the gaps and overlaps

    Oracle version: 11.2.0.3

    Platform: Linux

    EmpNo position start_dt end_dt

    1 X 01/01/2008-01/02/2010

    1 Y 01/02/2009-12/31/2012

    1 X 02/01/2012-04/01/2013

    1 Z 2012 12/31/5 / 01 / 2012

    1 O 01/05/2013 presents

    I need to find the exact period the employee has worked for the company in a particular situation. The query must be able to calculate the overlap/gaps as a result

    For example in this case, the result must be

    Days of position empno Total_Days

    1 x 1156 2943

    1 1543 2943 y

    1             z               244        2943

    Thank you

    Kevin

    SQL > select empno, position, hard

    2, sum (hard) earlier (partition by empno)

    3 of

    (4)

    5. Select empno, position, sum (end_dt - start_dt) hard

    6 t

    Group 7 by empno, position

    8)

    9;

    EMPNO P HARD TOT

    -------------------- - -------------------- --------------------

    1 X 1157 2944

    1 1543 2944 Y

    1 Z                  244                 2944

    Don't know exactly what you mean by cross-checking.  Can an employee cause two different positions at the same time?  Your sample data does not display it.

  • Grouping and then find the min and max

    DROP TABLE purge of y;

    CREATE TABLE y
    (
    ID NUMBER (10),
    START_DATE_TIME NUMBER (15),
    END_DATE_TIME NUMBER (15)
    );

    INSERT INTO y VALUES (1, 20140602164819, 20140606140851);
    INSERT INTO y VALUES (1, 20140827141743, 20140827142131);
    INSERT INTO y VALUES (1, 20140827141744, 20140827141835);
    INSERT INTO y VALUES (1, 20140827141744, 20140827142131);
    INSERT INTO y VALUES (1, 20140827141944, 20140827142131);
    INSERT INTO y VALUES (1, 20140827141944, 20140924142131);
    INSERT INTO y VALUES (1, 20140927141944, 20141027142131);
    INSERT INTO y VALUES (2, 20140602164819, 20140606140851);
    INSERT INTO y VALUES (2, 20140827141743, 20140827142131);
    INSERT INTO y VALUES (2, 20140827141744, 20140827141835);
    INSERT INTO y VALUES (2, 20140827141744, 20140827142131);
    INSERT INTO y VALUES (2, 20140827141944, 20140827142131);
    INSERT INTO y VALUES (2, 20140827141944, 20140924142131);
    INSERT INTO y VALUES (2, 20140927141944, 20141027142131);

    COMMIT;

    Select
    *
    Of
    There
    order by 2, 3;

    out necessary: when the output of the table are sorted by start_date_time and end_date_time in ascending order.
    and when the value of for a particular id start_date_time is located between the
    another set of values start_date_time and end_date_time for the same id then min (start_date_time) and max (end_date_time) is the desired output.

    and if there is no overlap then (start_date_time) min and max (end_date_time) is the output desired.

    and for each line of the output in ascending order of line number order addd;

    Example output is in the output table named

    drop table output is serving;

    create an output table
    (
    ID NUMBER (10),
    START_DATE_TIME NUMBER (15),
    END_DATE_TIME NUMBER (15),
    ROW_NUMBER NUMBER (10)
    );


    Values to INSERT OUTPUT (1, 20140602164819, 20140606140851, 1);
    Values to INSERT OUTPUT (1, 20140827141743, 20140924142131, 2);
    Values to INSERT OUTPUT (1, 20140927141944, 20141027142131, 3);
    Values to INSERT OUTPUT (2, 20140602164819, 20140606140851, 1);
    Values to INSERT OUTPUT (2, 20140827141743, 20140924142131, 2);
    Values to INSERT OUTPUT (2, 20140927141944, 20141027142131, 3);

    COMMIT;

    SELECT
    *
    Of
    output
    ORDER BY 1,2,3,4;

    Thank you

    SQL_Novice

    Hello

    Your postal code.  It is difficult to tell what you're doing wrong without knowing what you are doing.

    user6166680 wrote:

    Hi Frank:

    Max works for ID 3 values only because there is only one group here, as all other values compared, they overlap with max (end_date_time);

    but when I use MAX for ID 1 and 2, the result set is not the same thing as LAG works because there is a GAP...

    Exactly, you have to find the gaps.  If start_date_time is less than or equal to the MAXIMUM of all the end_date_times earlier, then it isn't a gap; Otherwise, there is a gap.

    What is the problem with MAX?

    WITH got_new_group AS

    (

    SELECT id, start_date_time and end_date_time

    CASE

    WHEN start_date_time <= > MAX (end_date_time) OVER (PARTITION BY id )

    ORDER BY start_date_time

    end_date_time

    ROWS BETWEEN UNBOUNDED PRECEDING

    AND 1 PRECEDING

    )

    THEN 0

    1. OTHER

    END AS new_group

    OF y

    )

    got_group_number AS

    (

    SELECT id, start_date_time and end_date_time

    SUM (new_group) OVER (PARTITION BY ID.

    ORDER BY start_date_time

    end_date_time

    ) AS group_number

    OF got_new_group

    )

    SELECT id

    MIN (start_date_time) AS group_start_date_time

    MAX (end_date_time) AS group_end_date_time

    group_number

    OF got_group_number

    GROUP BY id, group_number

    ORDER BY id, group_number

    ;

    You will notice that it is exactly what I posted earlier, in response #5, except that instead of using the OFFSET it uses MAX.

    Output:

    ID GROUP_START_DATE_TIME GROUP_END_DATE_TIME GROUP_NUMBER

    --------------- --------------------- ------------------- ---------------

    1 20140602164819 20140606140851 1

    1 20140827141743 20140924142131 2

    1 20140927141944 20141027142131 3

    2 20140602164819 20140606140851 1

    2 20140827141743 20140924142131 2

    2 20140927141944 20141027142131 3

    3 20140330041350 47121231000000 1

  • find the gaps between the sequences

    Hi all

    I have a request to find the gap between the sequences.

    Here's my table structure

    user_number | seq_number

    1            |   101

    1            |      102

    1            |    109

    1            |    110

    1            |    111

    2            |    201

    2            |    202

    2            |    203

    2            |    207

    2            |    208

    .            |    .

    .            |    .

    .            |    .

    .            |    .

    Now I have to find the gap between sequences, what I mean by gap's user_number 1 ' 6 between seq_number gap ' 102 'and 109'.

    Here's the query that I'm writing, but not giving the correct answer, he always gives the gap of 2:

    Select gap_start - gap_end gap

    de)

    Select a.seq_number + 1 as gap_start, b.seq_number - 1 as gap_end

    from my_table a, b from my_table

    where a.seq_number = b.seq_number

    and a.seq_number - (select c.seq_number

    from my_table c

    where c.seq_number = a.seq_number - 1) is set to null

    and a.user_number = '1'

    order of a.seq_number

    );

    Thank you.

    Hello

    Whenever you have a question, please post a small example of data (CREATE TABLE and only relevant columns, INSERT statements) for all of the tables involved and the accurate results you want from this data, so that people who want to help you can recreate the problem and test their ideas.

    Explain, using specific examples, how you get these results from these data.

    Always say what version of Oracle you are using (for example, 11.2.0.2.0).

    See the FAQ forum: Re: 2. How can I ask a question on the forums?

    Maybe you want something like this:

    WITH got_prev_seq_number AS

    (

    SELECT user_number, seq_number

    LAG (seq_numebr) OVER (PARTITION BY user_number

    ORDER BY seq_number

    ) AS prev_seq_number

    FROM my_table

    )

    SELECT user_number, seq_number, prev_seq_number

    OF got_prev_seq_number

    WHERE seq_number > prev_seq_number + 1

    ORDER BY user_number, seq_number

    ;

    .

  • Toshiba 40TL938G - find the date and time settings

    I have a 40TL938G and I can't find the date and time settings.
    Can someone help me?

    SSY
    I read the manual and it does ' t say anything about these settings.

    THX

    In the settings of digital television, you should see the option called local time settings

    To display the time on the TV you press the sign of the small clock on the remote control while watching a normal TV show (analog only)

  • Find the Center and angle

    Hello. I have two markers. It is stationary and the other is moving (app.jpg). How can I find the centers and angles shown in picture (app.jpg)?


  • How to find the utilities and windows drivers VGN-CR36G 8?

    Hello

    I use laptop Vaio VGN-CR36G with vista ultimate. Now, I want to install windows 8 enterprise edition.

    But I can't find the drivers and utilities on the site of sony vaio.

    But my product is compatible for windows 8 OS.

    Here is the link below:

    http://www.Sony-Asia.com/support/product/VGN-CR36G

    Can someone please help me to download all drivers and software?

    Thank you

    Hi Whiteeasy.

    This model does not support Windows 8 operating system which you can confirm from the link below.

    http://www.Sony-Asia.com/microsite/VAIO/support/GetWindows8/GetWindows8_EN_NA.html?referer=http%3A%2...

    If my post answered your question, please mark it as an "accepted Solution".

  • How to find the Maxima and Minima for each column of a 2D array?

    Hello

    I have a 2D chart and I would find the maxima and minima of each column of the 2-D table. Even though I know how to get maxima and minima for the whole picture but don't know how columnwise? Any ideas please?

    Thank you

    Rohit

    Hello

    @Smercurio-What you said is true, I should have shown using automatic indexing enabled which is really excellent choice. I just tried to show in a very simple way.

    Anyway, here's the best way

  • How to find the maximum and minimum of a waveform between cursors

    Hello!

    Does anyone know an easy way to find the maximum and minimum of a waveform between two sliders?

    Kind regards

    Andreas

    Hi Andreas,

    attached, you get an example of it in MeasurementStudio.

  • Cocomputer cannot find the printer and I do not have a disk for it

    Original title: printer SUPPORT

    my computer cannot find the printer and I do not have a disk for it

    Then, download and install the appropriate drivers on the printer manufacturer's support web site.

  • I have a U135 DX netbook with a built-in webcam. How can I find the cam and I press these keys for easy access

    I have a U135 DX netbook with a built-in webcam. How can I find the cam and I press those keys for easier access please PS I'm not a manual.

    You can get a manual here.

    http://www.msi.com/product/NB/U135DX.HTML#/?DIV=manual

  • Query to find the Pages and the list of permissions associated with a particular role in PS

    I want a query to find the Pages and the list of permissions associated with specific roles in PS.

    For example, if we see the role of manager accounts payable, it conatins Pages and the list of permissions.

    But, to get everything in EXCEL sheet by Manuel priocess's BIG job. So, can someone give me the query.

    Please try under queries

    The roles assigned to the list of Perm:

    SELECT B.ROLENAME

    OF PSCLASSDEFN A, PSROLECLASS B

    WHERE (A.CLASSID = B.CLASSID

    AND A.CLASSID =: 1).

    List of Perm pages can access:

    SELECT B.MENUNAME, B.BARNAME, B.BARITEMNAME, B.PNLITEMNAME, C.PAGEACCESSDESCR, B.DISPLAYONLY

    OF PSCLASSDEFN A, PSAUTHITEM B, PSPGEACCESSDESC C

    WHERE (A.CLASSID = B.CLASSID

    AND A.CLASSID =: 1

    AND B.BARITEMNAME > ' '

    AND B.AUTHORIZEDACTIONS = C.AUTHORIZEDACTIONS)

  • Where can I find the vertical and horizontal spaces of you or margins on CC Version?.

    Where can I find the vertical and horizontal spaces of you or margins on the CC Version? I need to leave a few spaces pixels around an image, but I can't find it on this new version.

    Thanks in advance

    In the CSS designer under window > CSS designer or SHIFT + F11

    Choose the item that you are working with and press on the + in the selectors of section

    Then in the Properties section, make sure that the first tab is selected (Layout) and scroll down to the margin

    Set it to a number of pixels that you want (for example, 10px)

  • To find the months and days between 2 dates

    Hello

    I want to find the months and days between 2 dates.

    For example.

    1 - Date: August 25, 2013

    2 - Date: October 23, 2013

    If we consider each month 30 days, it should give

    August 25, 2013 to August 30, 2013 = 6 days

    01-Sep-2013-30-Sep-2013 = 1 month

    October 23, 2013 to October 30, 2013 = 8 days

    Total = 1 month and 14 days.

    Kindly help as soon as possible.

    Thanks and greetings

    Suresh

    Assuming that d2 > d1,.

    where d)

    Select sysdate d1, sysdate + 56 double d2

    Union all select to_date (March 1, 2013 ',' dd-mon-yyyy "") d1, to_date (March 31, 2013 ',' dd-mon-yyyy ') d2 double

    Union all select to_date (5 February 2013 ',' dd-mon-yyyy ') d1, to_date (March 31, 2013 ',' dd-mon-yyyy "") double d2

    Union all select to_date (February 25, 2013 ',' dd-mon-yyyy "") d1, to_date (March 23, 2013 ',' dd-mon-yyyy ') d2 double

    Union all select to_date (February 25, 2013 ',' dd-mon-yyyy ') d1, to_date (March 31, 2013 ',' dd-mon-yyyy "") double d2

    Union all select to_date (August 2, 2013 ',' dd-mon-yyyy "") d1, to_date (29 October 2013 ',' dd-mon-yyyy ') d2 double

    Union all select to_date (February 1, 2013 ',' dd-mon-yyyy "") d1, to_date (May 31, 2013 ',' dd-mon-yyyy ') d2 double

    Union all select to_date (25 August 2013 ',' dd-mon-yyyy "") d1, to_date ('03-Sep-2013', 'Mon-dd-yyyy') d2 double

    Union all select to_date (July 30, 2013 ',' dd-mon-yyyy "") d1, to_date (August 31, 2013 ',' dd-mon-yyyy ') d2 double

    Union all select to_date (July 31, 2013 ',' dd-mon-yyyy ') d1, to_date (August 30, 2013 ',' dd-mon-yyyy "") double d2

    Union all select to_date (July 31, 2013 ',' dd-mon-yyyy ') d1, to_date (3 August 2013 ',' dd-mon-yyyy "") double d2

    Union all select to_date (3 July 2013 ',' dd-mon-yyyy "") d1, to_date (August 31, 2013 ',' dd-mon-yyyy ') d2 double

    Union all select to_date ('31-08-2013', ' dd-mm-yyyy'), to_date('05-10-2013','dd-mm-yyyy') of the double

    Union all select to_date ('05-02-2013', ' dd-mm-yyyy'), to_date('31-03-2013','dd-mm-yyyy') of the double

    Union all select to_date ('05-02-2013', ' dd-mm-yyyy'), to_date('05-03-2013','dd-mm-yyyy') of the double

    Union all select to_date ('05-02-2013', ' dd-mm-yyyy'), to_date('05-02-2013','dd-mm-yyyy') of the double

    )

    Select d1, d2,

    1 + 30 * trunc (months_between (d2, d1)) + LESS (extract (day of d2), 30)-LESS (excerpt (d1 day), 30)

    + CASE when extracted (d2 day)< extract(day="" from="" d1)="" then="" 30="" else="" 0="" end ="">

    d

    D1 D2 DAYSBETWEEN

    ----------- ----------- -----------

    October 10, 2013 5 December 2013 56

    March 1, 2013 30 March 31, 2013

    5 February 2013 March 31, 2013 56

    February 25, 2013 March 23, 2013 29

    February 25, 2013 March 31, 2013 36

    August 2, 2013 29 October 2013 88

    February 1, 2013 may 31, 2013 120

    August 25, 2013 03 - Sep-2013 9

    July 30, 2013 31 August 31, 2013

    July 31, 2013 August 30, 2013 31

    July 31, 2013 3 August 2013 4

    July 3, 2013 August 31, 2013 58

    31 August 2013 5 October 2013 36

    5 February 2013 March 31, 2013 56

    5 February 2013 March 5, 2013 31

    February 5, 2013 February 5, 2013 1

    In my view, which corresponds to your rules.

  • I posted a question can not find the question and or response. Help, please

    I posted a question can not find the question and or response. Help, please

    AFAIK, you can't share a catalog like this.

    The only recommended method is to store your images and catalogue on an external drive, you can alternatively plug in both computers.

    With today the external USB drives, the speed is still correct, and many users are satisfied with this solution.

Maybe you are looking for

  • How can I delete threads?

    I added by mistake the messages in a thread. I'm in trouble like I said to someone I had not received their message and it was hidden in a thread. I do not wish to use this feature and do not know how to get rid of said of the thread messages. I see

  • Is the fopen() function works in labview?

    Hi gentlemen... I used the fopen() function in labview, it does not work. Is that I can use the fopen() function works in labview? Please help me as I'm new to labview. Thanks in advance

  • Taking USB Flash drive supported Mass Storage Class Ver 1.1

    Hello I would like to know if the 128GB sandisk USB MSC Version 1.1 compatible player USB flash? If supported, is what model? Thank you very much for your advice. Kind regards Hong

  • HP Laserjet 5000 with Mac OSX 10.8.5

    I have a HP Laserjet 5000N printer. It was working fine but stopped communicating with my Mac - OSX 10.8.5. I have installed the latest driver, made that it recharges, etc..  I printed the configuration pages. Any advice would be greatly appreciated.

  • Battery issue classic blackBerry

    BB dear Experts,I bought a BB classic on February 2015 and endMarch 2015 my classic BB cannot be reactivated. Unfortunately I havebought this phone in Mississauga, Ontario, Canada and broughtwith me to work on the Viet Nam. There is however a BB stor