当前位置: 首页 > 面试题库 >

当偏移量可变时,如何捕获“ ext”事件?

孟晋
2023-03-14
问题内容

我在Oracle数据库中有一个交易表。我正在尝试为涉及多种交易类型的交付系统收集一份报告。实际上,“请求”类型可以是四个子类型之一(在此示例中为“ A”,“
B”,“ C”和“ D”),而“传递”类型可以是四个不同子类型之一类型(“ PULL”,“ PICKUP”,“
MAIL”)。从“请求”到“交付”之间可以有1到5个事务,并且“交付”类型中的许多也是中间事务。

Item | Transaction | Timestamp
001  | REQ-A       | 2014-07-31T09:51:32Z
002  | REQ-B       | 2014-07-31T09:55:53Z
003  | REQ-C       | 2014-07-31T10:01:15Z
004  | REQ-D       | 2014-07-31T10:02:29Z
005  | REQ-A       | 2014-07-31T10:05:47Z
002  | PULL        | 2014-07-31T10:20:04Z
002  | MAIL        | 2014-07-31T10:20:06Z
001  | PULL        | 2014-07-31T10:22:21Z
001  | TRANSFER    | 2014-07-31T10:22:23Z
003  | PULL        | 2014-07-31T10:24:10Z
003  | TRANSFER    | 2014-07-31T10:24:12Z
004  | PULL        | 2014-07-31T10:26:28Z
005  | PULL        | 2014-07-31T10:28:42Z
005  | TRANSFER    | 2014-07-31T10:28:44Z
001  | ARRIVE      | 2014-07-31T11:45:01Z
001  | PICKUP      | 2014-07-31T11:45:02Z
003  | ARRIVE      | 2014-07-31T11:47:44Z
003  | PICKUP      | 2014-07-31T11:47:45Z
005  | ARRIVE      | 2014-07-31T11:49:45Z
005  | PICKUP      | 2014-07-31T11:49:46Z

我需要的是像这样的报告:

Item | Start Tx | End Tx | Time
001  | REQ-A    | PICKUP | 1:53:30
002  | REQ-B    | MAIL   | 0:24:13
003  | REQ-C    | PICKUP | 1:46:30
004  | REQ-D    | PULL   | 0:23:59
005  | REQ-A    | PICKUP | 1:43:59

我有的:

Item | Start Tx | End Tx   | Time
001  | REQ-A    | PULL     | 0:30:49
001  | REQ-A    | TRANSFER | 0:30:51
001  | REQ-A    | ARRIVE   | 1:53:29
001  | REQ-A    | PICKUP   | 1:53:30
002  | REQ-B    | PULL     | 0:24:11
002  | REQ-B    | MAIL     | 0:24:13
003  | REQ-C    | PULL     | 0:22:55
003  | REQ-C    | TRANSFER | 0:22:57
003  | REQ-C    | ARRIVE   | 1:46:29
003  | REQ-C    | PICKUP   | 1:46:30
004  | REQ-D    | PULL     | 0:23:59
005  | REQ-A    | PULL     | 0:22:55
005  | REQ-A    | TRANSFER | 0:22:57
005  | REQ-A    | ARRIVE   | 1:43:58
005  | REQ-A    | PICKUP   | 1:43:59

我正在做什么以获取数据:

SELECT Item, Transaction, nextTransaction, nextTimestamp - Timestamp
FROM (
    SELECT Item, Transaction, Timestamp,
      LEAD(Transaction, 5) OVER (PARTITION BY Item ORDER BY Timestamp) AS "nextTransaction"
      LEAD(Timestamp, 5) OVER (PARTITION BY Item ORDER BY Timestamp) AS "nextTimestamp"
    FROM Transactions
    UNION ALL 
    SELECT Item, Transaction, Timestamp,
      LEAD(Transaction, 4) OVER (PARTITION BY Item ORDER BY Timestamp) AS "nextTransaction"
      LEAD(Timestamp, 4) OVER (PARTITION BY Item ORDER BY Timestamp) AS "nextTimestamp"
    FROM Transactions
    UNION ALL 
    SELECT Item, Transaction, Timestamp,
      LEAD(Transaction, 3) OVER (PARTITION BY Item ORDER BY Timestamp) AS "nextTransaction"
      LEAD(Timestamp, 3) OVER (PARTITION BY Item ORDER BY Timestamp) AS "nextTimestamp"
    FROM Transactions
    UNION ALL 
    SELECT Item, Transaction, Timestamp,
      LEAD(Transaction, 2) OVER (PARTITION BY Item ORDER BY Timestamp) AS "nextTransaction"
      LEAD(Timestamp, 2) OVER (PARTITION BY Item ORDER BY Timestamp) AS "nextTimestamp"
    FROM Transactions
    UNION ALL 
    SELECT Item, Transaction, Timestamp,
      LEAD(Transaction, 1) OVER (PARTITION BY Item ORDER BY Timestamp) AS "nextTransaction"
      LEAD(Timestamp, 1) OVER (PARTITION BY Item ORDER BY Timestamp) AS "nextTimestamp"
    FROM Transactions
)
WHERE nextTransaction IS NOT NULL
AND Transaction IN ('REQ-A', 'REQ-B', 'REQ-C', 'REQ-D')

我可以在脚本中手动解析它(也许这实际上是最好的做法),但是为了学习起见,我想知道是否有可能单独使用SQL实际做到这一点。


问题答案:

听起来您想要基于时间的第一个和最后一个事务。我认为以下是您想要的:

select item,
       min(transaction) keep (dense_rank first order by timestamp) as StartTx, 
       min(transaction) keep (dense_rank last order by timestamp) as EndTx,
       max(timestamp) - min(timestamp)
from transactions t
group by item;


 类似资料:
  • 我已经实现了下面链接中的代码,用于从事件中心接收事件。但是假设有10个事件,每5个事件检查一次。现在程序在读取第7个事件时异常退出,如果我再次重启事件处理器主机,那么事件(1,2,3,4,6)将被重新读取。请建议我如何再次避免重读和阅读第7次事件?任何例子都值得欣赏。谢了。 https://github.com/Azure/azure-event-hubs/blob/master/samples/

  • 问题内容: 我想捕获此错误: 编辑: 实际上,我在以下行中收到此错误: 问题答案: 您需要定义您的自定义错误处理程序,例如:

  • 问题内容: 我知道相反。给定一个时区,我可以通过以下代码片段获取时区偏移量: 我想知道如何从时区偏移量获取时区名称。 鉴于 (以毫秒为单位; +6.00偏移) 我想得到以下任何可能的时区名称的结果: 问题答案: 用

  • 这将base64打印到控制台: 来源:https://stackoverflow.com/A/36281449/1063287 我希望能够将base64赋值给一个变量,所以我尝试了以下操作,基于这个答案: 它目前没有向控制台打印任何内容。 问题: 我正在提交一个表单在谷歌应用程序脚本环境。 我以前已经这样做了,并将一个表单对象(包括一个文件)传递给Google Apps脚本函数。 但是,这种方法的

  • 问题内容: 我在Java中有一个应用程序,其中我尝试确保如果有人在代码中退出代码System.exit(),则应调用侦听器来执行某些操作,例如记录消息并释放资源… 我如何实施它,欢迎任何建议/方法。 问题答案: 该方法可用于添加一个关闭钩子,该钩子基本上是未启动的,该钩子在Java虚拟机关闭时执行。 但是,这是应该谨慎对待的领域,因为它是在JVM生命周期的非常敏感的时间执行的。从API规范中获取该