2

我正在使用事务表。我想生成一些变量来确定同一客户在过去 5 分钟和 30 分钟以及 1、2、3 和 4 小时以及 1、2、5 和 10 天内执行的交易数量。在事务表中生成此类列的有效方法是什么?-- 解决方案将是一个运行总计,直到每个事务时间。我可以使用 Oracle 和其他 SQL 变体。

4

2 回答 2

1

我认为您不想物理存储这些统计信息,因为它们会由于其时间性质而不断变化。解决方案实际上取决于您将如何使用这些统计信息。我可以想到两种主要方法:

  1. 针对给定唯一客户的临时查询 - 在这种情况下,我将有一个存储过程,将客户 ID 和时间范围作为参数,并返回每个客户的相关交易数量。

  2. 跨多个窗口的多个客户的“标准化”报告 - 在这种情况下,您可能希望查看每个时间段的视图,选择所有客户以及每个客户在适当时间段内的交易数量。

也许如果您可以向我们提供有关您的用例的更多信息,我们将能够更具体..

于 2011-08-30T12:30:33.243 回答
0

对于单个客户:

我能想出的最佳解决方案不是使用分析,而是使用子查询/公用表表达式。Oracle 通常足够聪明,知道是否将它们变成临时表,从而降低了多次传递相同数据的成本。

with txns as 
       (select customer_id
               txn_id,
               txn_ts
          from transaction_table
         where customer_id = ?
           AND txn_ts >= SYSTIMESTAMP - NUMTODSINTERVAL(10, 'DAY')
       )
select customer_id,
       (select count(*) from txns 
         where event_ts >= systimestamp - numtodsinterval(5/1440, 'day')) 
         as txn_5_min,
       (select count(*) from txns 
         where event_ts >= systimestamp - numtodsinterval(30/1440, 'day')) 
         as txn_30_min,
       (select count(*) from txns 
         where event_ts >= systimestamp - numtodsinterval(1/24, 'day')) 
         as txn_1_hour,
       (select count(*) from txns 
         where event_ts >= systimestamp - numtodsinterval(2/24, 'day')) 
         as txn_2_hour,
       (select count(*) from txns 
         where event_ts >= systimestamp - numtodsinterval(3/24, 'day')) 
         as txn_3_hour,
       (select count(*) from txns 
         where event_ts >= systimestamp - numtodsinterval(4/24, 'day')) 
         as txn_4_hour,
       (select count(*) from txns 
         where event_ts >= systimestamp - numtodsinterval(1, 'day')) 
         as txn_1_day,
       (select count(*) from txns 
         where event_ts >= systimestamp - numtodsinterval(2, 'day')) 
         as txn_2_day,
       (select count(*) from txns 
         where event_ts >= systimestamp - numtodsinterval(5, 'day')) 
         as txn_5_day,
       (select count(*) from txns 
         where event_ts >= systimestamp - numtodsinterval(10, 'day')) 
         as txn_10_day
  from customer
 WHERE customer_id = ?;

您将能够对多客户案例使用类似的实现,尽管效率肯定会受到影响。考虑在您完成检索时,您的 5 分钟级别数据是否对所有客户来说都是陈旧的。

于 2011-08-31T14:24:54.660 回答