使用 Oracle SQL 仅计算原始秒数

Count only original seconds with Oracle SQL

我有一个具有此结构和数据的 table,开始和停止位置为 audio/video。我得数原来的秒数,舍弃不原来的秒数。

例如

    CUSTOMER_ID ITEM_ID CHAPTER_ID  START_POSITION  END_POSITION
A   123456      1       6           0               97
B   123456      1       6           97              498
C   123456      1       6           498             678
D   123456      1       6           678             1332
E   123456      1       6           1180            1190
F   123456      1       6           1190            1206
G   123456      1       6           1364            1529
H   123456      1       6           1530            1531

Original Data

“E”和“F”行不代表原始秒数,因为“D”行从 678 开始到 1332 结束,所以我需要创建一组新的行,如下所示:

    CUSTOMER_ID ITEM_ID CHAPTER_ID  START_POSITION  END_POSITION
A   123456      1       6           0               97
B   123456      1       6           97              498
C   123456      1       6           498             678
D   123456      1       6           678             1332
E   123456      1       6           1364            1529
F   123456      1       6           1530            1531

New Result Set

你能帮我做这件事吗?

如果我没听错,你可以使用not exists过滤出范围包含在另一行范围内的行:

select t.*
from mytable t
where not exists (
    select 1
    from mytable t1
    where 
        t1.customer_id = t.customer_id
        and t1.start_position < t.start_position 
        and t1.end_position   > t.end_position
)

您可以按如下方式使用自连接:

Select distinct t.* 
  from your_table t
  Left Join your_table tt
    On t.customer_id = tt.customer_id
   And t.item_id = tt.item_id
   And t.chapter_id = tt.chapter_id
   And t.rowid <> tt.rowid
   And t.start_position between tt.start_position and tt.end_position - 1
 Where tt.rowid is null