简体   繁体   中英

Oracle sql: Find min max consecutive dates within same group

I know the question is probably badly explained, but I don't know how else to explain this. I have the following data: (ordered by date)

    DATE    GROUP
    11-Oct-16   A
    12-Oct-16   A
    13-Oct-16   A
    14-Oct-16   B
    15-Oct-16   B
    16-Oct-16   A
    17-Oct-16   A
    18-Oct-16   C
    19-Oct-16   C
    20-Oct-16   C
    21-Oct-16   C
    22-Oct-16   A
    23-Oct-16   A
    24-Oct-16   A

I want to find consecutive usage for groups. The results I want will explain this better than me:

    GROUP   MIN(DATE)   MAX(DATE)
    A   11-Oct-16   13-Oct-16
    B   14-Oct-16   15-Oct-16
    A   16-Oct-16   17-Oct-16
    C   18-Oct-16   21-Oct-16
    A   22-Oct-16   24-Oct-16

Any idea how to do this in oracle sql? Thank you.

This can be a way:

with test("DATE","GROUP") as
(
    select to_date('11-10-16', 'dd-mm-rr'),'A' from dual union all
    select to_date('12-10-16', 'dd-mm-rr'),'A' from dual union all
    select to_date('13-10-16', 'dd-mm-rr'),'A' from dual union all
    select to_date('14-10-16', 'dd-mm-rr'),'B' from dual union all
    select to_date('15-10-16', 'dd-mm-rr'),'B' from dual union all
    select to_date('16-10-16', 'dd-mm-rr'),'A' from dual union all
    select to_date('17-10-16', 'dd-mm-rr'),'A' from dual union all
    select to_date('18-10-16', 'dd-mm-rr'),'C' from dual union all
    select to_date('19-10-16', 'dd-mm-rr'),'C' from dual union all
    select to_date('20-10-16', 'dd-mm-rr'),'C' from dual union all
    select to_date('21-10-16', 'dd-mm-rr'),'C' from dual union all
    select to_date('22-10-16', 'dd-mm-rr'),'A' from dual union all
    select to_date('23-10-16', 'dd-mm-rr'),'A' from dual union all
    select to_date('24-10-16', 'dd-mm-rr'),'A' from dual
)
select min("DATE"), max("DATE"), "GROUP" 
from (
        select "DATE",
               "DATE" - row_number() over (partition by "GROUP" order by "DATE") as minDate,
               "GROUP"
        from test
)
group by "GROUP", minDate
order by "GROUP", minDate

The inner query builds the minimum date for a group of consecutive dates, while the external one simply aggregates by this minimum date, thus building a row for every group of consecutive dates.

As an aside, it's better to avoid using reserved words as column names.

So basically I think that the proper answer was in @mathguy comment. Here I am just expanding and giving the proper full query.

select GROUP_NAME, grp_id, min(date_field) as starting_date, max(date_field) as ending_date
from (
select DATE_FIELD, GROUP_NAME, 
row_number() over ( order by date_field) - row_number() over ( partition by group_name order by date_field ) as grp_id
from darber.my_table) innested
group by GROUP_NAME, grp_id
order by min(date_field);

Please note: I changed the field name because they were giving conflicts on my database. In this solution DATE is now DATE_FIELD and GROUP is now GROUP_NAME.

I found this to be a really smart and elegant solution. I hope will help others.

I had the same issue with multiple members in the group. In case you are running into the same issue, replace the GROUP_NAME with "groupMember1, groupMember2, ..."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM