簡體   English   中英

SQL計數連續的行

[英]SQL count consecutive rows

我在表中有以下數據:

|event_id    |starttime        |person_id|attended|
|------------|-----------------|---------|--------|
| 11512997-1 | 01-SEP-16 08:00 | 10001   | N      |
| 11512997-2 | 01-SEP-16 10:00 | 10001   | N      |
| 11512997-3 | 01-SEP-16 12:00 | 10001   | N      |
| 11512997-4 | 01-SEP-16 14:00 | 10001   | N      |
| 11512997-5 | 01-SEP-16 16:00 | 10001   | N      |
| 11512997-6 | 01-SEP-16 18:00 | 10001   | Y      |
| 11512997-7 | 02-SEP-16 08:00 | 10001   | N      |
| 11512997-1 | 01-SEP-16 08:00 | 10002   | N      |
| 11512997-2 | 01-SEP-16 10:00 | 10002   | N      |
| 11512997-3 | 01-SEP-16 12:00 | 10002   | N      |
| 11512997-4 | 01-SEP-16 14:00 | 10002   | Y      |
| 11512997-5 | 01-SEP-16 16:00 | 10002   | N      |
| 11512997-6 | 01-SEP-16 18:00 | 10002   | Y      |
| 11512997-7 | 02-SEP-16 08:00 | 10002   | Y      |

我想生成以下結果,其中返回atended ='N'的最大連續出現次數:

|person_id|consec_missed_max|
| 1001    | 5               |
| 1002    | 3               |

如何在Oracle(或ANSI)SQL中完成此操作? 謝謝!

編輯:

到目前為止,我嘗試過:

WITH t1 AS
(SELECT t.person_id,
    row_number() over(PARTITION BY t.person_id ORDER BY t.starttime) AS idx
    FROM the_table t
    WHERE t.attended = 'N'),
t2 AS
(SELECT person_id, MAX(idx) max_idx FROM t1 GROUP BY person_id)
SELECT t1.person_id, COUNT(1) ct
    FROM t1
    JOIN t2
    ON t1.person_id = t2.person_id
GROUP BY t1.person_id;

主要工作在因子子查詢“准備”中。 您似乎對分析功能有點熟悉,但這還不夠。 該解決方案使用所謂的“tabibitosan”方法在一個或多個維度中創建具有相同特征的連續行組; 在這種情況下,您希望將每個序列的連續N行分組為不同的組。 這是通過兩個ROW_NUMBER()調用的差異來完成的 - 一個是按人員划分的,另一個是按人划分並參加。 谷歌“tabibitosan”如果需要,可以閱讀更多關於這個想法的內容。

with
     inputs ( event_id, starttime, person_id, attended ) as (
        select '11512997-1', to_date('01-SEP-16 08:00', 'dd-MON-yy hh24:mi'), 10001, 'N' from dual union all
        select '11512997-2', to_date('01-SEP-16 10:00', 'dd-MON-yy hh24:mi'), 10001, 'N' from dual union all     
        select '11512997-3', to_date('01-SEP-16 12:00', 'dd-MON-yy hh24:mi'), 10001, 'N' from dual union all
        select '11512997-4', to_date('01-SEP-16 14:00', 'dd-MON-yy hh24:mi'), 10001, 'N' from dual union all
        select '11512997-5', to_date('01-SEP-16 16:00', 'dd-MON-yy hh24:mi'), 10001, 'N' from dual union all
        select '11512997-6', to_date('01-SEP-16 18:00', 'dd-MON-yy hh24:mi'), 10001, 'Y' from dual union all
        select '11512997-7', to_date('02-SEP-16 08:00', 'dd-MON-yy hh24:mi'), 10001, 'N' from dual union all
        select '11512997-1', to_date('01-SEP-16 08:00', 'dd-MON-yy hh24:mi'), 10002, 'N' from dual union all
        select '11512997-2', to_date('01-SEP-16 10:00', 'dd-MON-yy hh24:mi'), 10002, 'N' from dual union all
        select '11512997-3', to_date('01-SEP-16 12:00', 'dd-MON-yy hh24:mi'), 10002, 'N' from dual union all
        select '11512997-4', to_date('01-SEP-16 14:00', 'dd-MON-yy hh24:mi'), 10002, 'Y' from dual union all
        select '11512997-5', to_date('01-SEP-16 16:00', 'dd-MON-yy hh24:mi'), 10002, 'N' from dual union all
        select '11512997-6', to_date('01-SEP-16 18:00', 'dd-MON-yy hh24:mi'), 10002, 'Y' from dual union all
        select '11512997-7', to_date('02-SEP-16 08:00', 'dd-MON-yy hh24:mi'), 10002, 'Y' from dual
      ),
      prep ( starttime, person_id, attended, gp ) as (
        select starttime, person_id, attended,
               row_number() over (partition by person_id order by starttime) -
                   row_number() over (partition by person_id, attended 
                                      order by starttime)
        from   inputs
      ),
      counts ( person_id, consecutive_absences ) as (
        select person_id, count(*)
        from   prep
        where  attended = 'N'
        group by person_id, gp
     )
select person_id, max(consecutive_absences) as max_consecutive_absences
from   counts
group by person_id
order by person_id;

OUTPUT:

 PERSON_ID                MAX_CONSECUTIVE_ABSENCES
---------- ---------------------------------------
     10001                                       5
     10002                                       3

如果您使用的是Oracle 12c ,則可以使用MATCH_RECOGNIZE

數據:

CREATE TABLE data AS 
SELECT *
FROM (
with inputs ( event_id, starttime, person_id, attended ) as (
  select '11512997-1', to_date('01-SEP-16 08:00', 'dd-MON-yy hh24:mi'), 10001, 'N' from dual union all
  select '11512997-2', to_date('01-SEP-16 10:00', 'dd-MON-yy hh24:mi'), 10001, 'N' from dual union all     
  select '11512997-3', to_date('01-SEP-16 12:00', 'dd-MON-yy hh24:mi'), 10001, 'N' from dual union all
  select '11512997-4', to_date('01-SEP-16 14:00', 'dd-MON-yy hh24:mi'), 10001, 'N' from dual union all
  select '11512997-5', to_date('01-SEP-16 16:00', 'dd-MON-yy hh24:mi'), 10001, 'N' from dual union all
  select '11512997-6', to_date('01-SEP-16 18:00', 'dd-MON-yy hh24:mi'), 10001, 'Y' from dual union all
  select '11512997-7', to_date('02-SEP-16 08:00', 'dd-MON-yy hh24:mi'), 10001, 'N' from dual union all
  select '11512997-1', to_date('01-SEP-16 08:00', 'dd-MON-yy hh24:mi'), 10002, 'N' from dual union all
  select '11512997-2', to_date('01-SEP-16 10:00', 'dd-MON-yy hh24:mi'), 10002, 'N' from dual union all
  select '11512997-3', to_date('01-SEP-16 12:00', 'dd-MON-yy hh24:mi'), 10002, 'N' from dual union all
  select '11512997-4', to_date('01-SEP-16 14:00', 'dd-MON-yy hh24:mi'), 10002, 'Y' from dual union all
  select '11512997-5', to_date('01-SEP-16 16:00', 'dd-MON-yy hh24:mi'), 10002, 'N' from dual union all
  select '11512997-6', to_date('01-SEP-16 18:00', 'dd-MON-yy hh24:mi'), 10002, 'Y' from dual union all
  select '11512997-7', to_date('02-SEP-16 08:00', 'dd-MON-yy hh24:mi'), 10002, 'Y' from dual
      )
SELECT * FROM inputs
);   

並查詢:

SELECT PERSON_ID, MAX(LEN) AS MAX_ABSENCES_IN_ROW
FROM data
MATCH_RECOGNIZE (
   PARTITION BY PERSON_ID
   ORDER BY STARTTIME
   MEASURES FINAL COUNT(*) AS len
   ALL ROWS PER MATCH
   PATTERN(a b*)
   DEFINE b AS attended = a.attended
)
WHERE attended = 'N'
GROUP BY PERSON_ID;

輸出:

"PERSON_ID","MAX_ABSENCES_IN_ROW"
10001,5
10002,3

編輯:

正如@mathguy指出的那樣,它可以改寫為:

SELECT PERSON_ID, MAX(LEN) AS MAX_ABSENCES_IN_ROW
FROM data
MATCH_RECOGNIZE (
   PARTITION BY PERSON_ID
   ORDER BY STARTTIME
   MEASURES COUNT(*) AS len
   PATTERN(a+)
   DEFINE a AS attended = 'N'
)
GROUP BY PERSON_ID;

db <>小提琴演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM