简体   繁体   English

仅计算原始秒数 Oracle SQL

[英]Count only original seconds with Oracle SQL

I have a table with this structure and data, with start and stop positions of an audio/video.我有一个包含这种结构和数据的表格,其中包含音频/视频的开始和停止位置。 I have to count the original seconds and discard the not original ones.我必须计算原始秒数并丢弃非原始秒数。

Eg例如

    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原始数据

Lines "E" and "F" does not represent original seconds because "D" line starts at 678 and finishes with 1332 so I need to create a new set of lines like this: “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新结果集

Can you help mw with this?你能帮我做这件事吗?

If I am following you correctly, you can use not exists to filter out rows whose range is contained in the range of another row:如果我没听错,你可以使用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
)

You can use the self join as follows:您可以按如下方式使用自连接:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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