Oracle - создание группы по групповому интервалу - PullRequest
0 голосов
/ 24 февраля 2019

Как сделать запрос, который создаст группы, у которых пробел между ними будет больше, чем "n"?

Данные:

01-01-2000
02-01-2000
03-01-2000
06-01-2000
07-01-2000
19-02-2001
10-01-2002
11-01-2002

Я хотел бы получить результат для интервала между записями, например, 2 дня:

DATE           GROUP 

    01-01-2000    1
    02-01-2000    1
    03-01-2000    1

    06-01-2000    2    
    07-01-2000    2

    19-02-2001    3

    10-01-2002    4
    11-01-2002    4

Для 10 дней:

    01-01-2000    1
    02-01-2000    1
    03-01-2000    1        
    06-01-2000    1    
    07-01-2000    1

    19-02-2001    2

    10-01-2002    3
    11-01-2002    3

Другой пример с целыми числами:

with x as (
  select 1 as A from dual
  union all
  select 2 as A from dual
  union all
  select 3 as A from dual
  union all
  select 10 as A from dual
  union all
  select 20 as A from dual
  union all
  select 22 as A from dual
  union all
  select 33 as A from dual
  union all
  select 40 as A from dual
  union all
  select 50 as A from dual
  union all
  select 100 as A from dual
  union all
  select 101 as A from dual
  union all
  select 102 as A from dual
) select A
from x;

Мне нужно создать группы для увеличения значения более чем на 3:

Пример результата:

1     1
2     1
3     1
10    2
20    3
22    3
33    4
40    5
50    6
100   7
101   7
102   7

Ответы [ 2 ]

0 голосов
/ 24 февраля 2019

Пример с целым числом:

with x as (
  select 1 as A from dual
  union all
  select 2 as A from dual
  union all
  select 3 as A from dual
  union all
  select 10 as A from dual
  union all
  select 20 as A from dual
  union all
  select 22 as A from dual
  union all
  select 33 as A from dual
  union all
  select 40 as A from dual
  union all
  select 50 as A from dual
  union all
  select 100 as A from dual
  union all
  select 101 as A from dual
  union all
  select 102 as A from dual
) SELECT A, SUM(NEW_GROUP) OVER ( ORDER BY A) AS GROUPE FROM (
  SELECT 
    A,
    CASE WHEN A - LAG(A,1,1) OVER ( ORDER BY A) > 5 THEN 1 ELSE 0 END AS NEW_GROUP
    FROM X
)
order by A;
0 голосов
/ 24 февраля 2019

Вот один из способов сделать это

CREATE TABLE TEST ( 
  DATE_IN DATE
  );

INSERT INTO TEST VALUES (TO_DATE('01-01-2000','DD-MM-YYYY'));
INSERT INTO TEST VALUES (TO_DATE('02-01-2000','DD-MM-YYYY'));
INSERT INTO TEST VALUES (TO_DATE('03-01-2000','DD-MM-YYYY'));
INSERT INTO TEST VALUES (TO_DATE('06-01-2000','DD-MM-YYYY'));
INSERT INTO TEST VALUES (TO_DATE('07-01-2000','DD-MM-YYYY'));
INSERT INTO TEST VALUES (TO_DATE('19-02-2001','DD-MM-YYYY'));
INSERT INTO TEST VALUES (TO_DATE('10-01-2002','DD-MM-YYYY'));
INSERT INTO TEST VALUES (TO_DATE('11-01-2002','DD-MM-YYYY'));

--HERE IS AN EXAMPLE FOR 1 DAY. Just change the value in the > 1 TO >10 
--if you want to create a group if there is a gap of more than 10days 
SELECT DATE_IN, SUM(NEW_GROUP) OVER ( ORDER BY DATE_IN) AS GROUPE FROM (
  SELECT 
    DATE_IN,
    CASE WHEN DATE_IN - LAG(DATE_IN,1,TO_DATE('01-01-1900','DD-MM-YYYY')) OVER ( ORDER BY DATE_IN) > 1 THEN 1 ELSE 0 END AS NEW_GROUP
    FROM TEST
)

-- Result
DATE_IN GROUPE
2000-01-01T00:00:00Z    1
2000-01-02T00:00:00Z    1
2000-01-03T00:00:00Z    1
2000-01-06T00:00:00Z    2
2000-01-07T00:00:00Z    2
2001-02-19T00:00:00Z    3
2002-01-10T00:00:00Z    4
2002-01-11T00:00:00Z    4
...