【问题标题】:Count only original seconds with Oracle SQL使用 Oracle SQL 仅计算原始秒数
【发布时间】:2020-11-11 15:44:32
【问题描述】:

我有一个具有这种结构和数据的表,其中包含音频/视频的开始和停止位置。我必须计算原始秒数并丢弃非原始秒数。

例如

    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

你能帮我解决这个问题吗?

【问题讨论】:

  • 目前还不清楚您如何决定将哪些值用于开始和结束位置。

标签: sql oracle subquery


【解决方案1】:

如果我没听错的话,你可以使用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
)

【讨论】:

    【解决方案2】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-22
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多