【问题标题】:SQL get the most recent duplicate entry with timestampSQL 获取最新的带有时间戳的重复条目
【发布时间】:2018-11-29 10:40:34
【问题描述】:

我有下表:

ID          DESCRIPTION                 TIMESTAMP
1            RECEIVER                    00:10:00
1            SENDER                      00:08:00
1            EXECUTOR                    00:05:00
1            SENDER                      00:03:00

如何获取最新“SENDER”描述并使用我的时间戳找到与另一个描述的时差?

我想找出 SENDER 和 EXECUTOR 之间的时间差,但我得到了奇怪的结果,因为它同时拾取了两个 SENDER 条目。

谢谢 亚当

【问题讨论】:

  • 预期结果是什么? (附上表数据。)
  • 是否有 ID 不是 1 的行?
  • @jarlh 我的表有数百万个 ID,但我把那个表放在一起,就像我选择 ID=1 一样,应该说抱歉。
  • 添加更多的样本数据行,至少有一个其他 id。没有 SENDER 行的 ID 的预期结果是什么?有 3 个 SENDER 行等?
  • 没有“时间”数据类型是Oracle?你不知道它是哪种数据类型?您希望我们在不提供所需信息的情况下如何回答?在提问之前通读stackoverflow.com/help/how-to-ask 以了解如何提问。

标签: sql oracle


【解决方案1】:
SELECT
    t1.timestamp - t2.timestamp
from
    (SELECT 
        timestamp 
    FROM 
        table 
    WHERE 
        description='SENDER' 
    ORDER BY timestamp DESC LIMIT 1) t1,
    table t2
WHERE
    t2.description = 'your_description'

【讨论】:

    【解决方案2】:

    您可以通过使用lagrow_number 函数来使用这种机制:

    select id, timestamp_diff
      from
    (
      with t(ID,DESCRIPTION,TIMESTAMP) as
      (
       select 1,'RECEIVER',to_timestamp('00:10:00','HH24:MI:SS') from dual union all
       select 1,'SENDER',to_timestamp('00:08:00','HH24:MI:SS') from dual union all
       select 1,'EXECUTOR',to_timestamp('00:05:00','HH24:MI:SS') from dual union all
       select 1,'SENDER',to_timestamp('00:03:00','HH24:MI:SS') from dual 
      )
       select t.id, 
              t.timestamp - lag(t.timestamp) over (order by t.timestamp desc) as timestamp_diff,
              row_number() over (order by t.timestamp) as rn 
         from t   
        where t.description = 'SENDER'
    )
    where rn = 1;
    
     ID    TIMESTAMP_DIFF
     -- --------------------
     1  -000000000 00:05:00
    

    对于多个 ID,请考虑使用以下 ID:

    select id , max(timestamp_diff) as timestamp_diff
      from
      (
        with t(ID,DESCRIPTION,TIMESTAMP) as
        (
         select 1,'RECEIVER',to_timestamp('00:10:00','HH24:MI:SS') from dual union all
         select 1,'SENDER',to_timestamp('00:08:00','HH24:MI:SS') from dual union all
         select 1,'EXECUTOR',to_timestamp('00:05:00','HH24:MI:SS') from dual union all
         select 1,'SENDER',to_timestamp('00:03:00','HH24:MI:SS') from dual union all
         select 2,'SENDER',to_timestamp('00:06:00','HH24:MI:SS') from dual union all
         select 2,'SENDER',to_timestamp('00:02:00','HH24:MI:SS') from dual 
        )
         select t.id, 
                t.timestamp - lag(t.timestamp) over 
                (partition by t.id order by t.id,t.timestamp desc) as timestamp_diff,
                row_number() over (order by t.id,t.timestamp) as rn,
                t.description 
           from t   
          where t.description = 'SENDER'
      )  
      group by id, description;
    
     ID    TIMESTAMP_DIFF
     -- --------------------
     1  -000000000 00:05:00
     2  -000000000 00:04:00
    

    【讨论】:

      猜你喜欢
      • 2021-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-10
      • 1970-01-01
      • 1970-01-01
      • 2017-05-12
      • 2011-12-24
      相关资源
      最近更新 更多