oracle SQL选择过去x天滚动期间的不同客户

时间:2017-02-16 05:28:51

标签: sql oracle oracle11g window-functions analytic-functions

说你有一张顾客表,日期如下:
[customer_table]

+----------+-----------+----------+
| customer | date      | purchase |
+----------+-----------+----------+
| 1        | 1/01/2016 | 12       |
+----------+-----------+----------+
| 1        | 1/12/2016 | 3        |
+----------+-----------+----------+
| 2        | 5/03/2016 | 5        |
+----------+-----------+----------+
| 3        | 1/16/2016 | 6        |
+----------+-----------+----------+
| 3        | 3/22/2016 | 1        |
+----------+-----------+----------+  

我想写一个查询来计算在过去10天内有多少不同的客户作为滚动期,从每个日历日开始,向后计算10天。因此,对于2016年的每个独特日,最终输出将是日历,其中每天具有在日历的当天的前10天中存在的不同客户的计数,如下所示:
[result_table]

+-----------+------------------+
| date      | unique customers |
+-----------+------------------+
| 1/01/2016 | 112              |
+-----------+------------------+
| 1/02/2016 | 104              |
+-----------+------------------+
| 1/03/2016 | 140              |
+-----------+------------------+
| 1/04/2016 | 133              |
+-----------+------------------+
| ....      | 121              |
+-----------+------------------+  

我想出的一个解决方案是创建一个单列的日历表,然后使用不等式连接将日历表连接到customer表。我认为这是非常低效的,我正在寻求更快的解决方案。所以我的第一步是创建一个这样的日历:
[日历]

+-----------+
| date      |
+-----------+
| 1/01/2016 |
+-----------+
| 1/02/2016 |
+-----------+
| 1/03/2016 |
+-----------+
| 1/04/2016 |
+-----------+
| 1/05/2016 |
+-----------+  

然后,对于该日历中的每一天,要计算每天之前的不同客户群,我加入了这样的不平等:

select
count(distinct customer) as unique customers
from calendar c
left join mytable m
on c.date>=m.date and m.date>=c.date-10  

虽然我认为这是正确的,但它的运行速度非常慢(比如2年的日历,有几百万客户)。是否有可以帮助我的oracle分析函数?

1 个答案:

答案 0 :(得分:3)

  

是否有可以帮助我的oracle分析函数?

不是真的 - 来自COUNT() documentation

  

如果您指定DISTINCT,则只能指定analytic_clause的query_partition_clause。 <{1}}和order_by_clause是不允许的。

您可能需要windowing_clauseDISTINCT,这是不允许的。

<强>更新

使用由客户划分的非windowing_clause分析查询和按天聚合的组合,可以获得与无效语法相同的效果:

Oracle安装程序

DISTINCT

<强>查询

注意:下面的查询仅针对一个月的数据,并且在前两天的范围内说明原则,但很容易将参数更改为12个月和10天。 < / p>

CREATE TABLE table_name ( customer, dt ) AS
  SELECT 1, DATE '2017-01-10' FROM DUAL UNION ALL
  SELECT 1, DATE '2017-01-11' FROM DUAL UNION ALL
  SELECT 1, DATE '2017-01-15' FROM DUAL UNION ALL
  SELECT 1, DATE '2017-01-20' FROM DUAL UNION ALL
  SELECT 2, DATE '2017-01-12' FROM DUAL UNION ALL
  SELECT 2, DATE '2017-01-19' FROM DUAL UNION ALL
  SELECT 3, DATE '2017-01-10' FROM DUAL UNION ALL
  SELECT 3, DATE '2017-01-13' FROM DUAL UNION ALL
  SELECT 3, DATE '2017-01-15' FROM DUAL UNION ALL
  SELECT 3, DATE '2017-01-20' FROM DUAL;

<强>输出

SELECT day,
       SUM( has_order_in_range ) AS unique_customers
FROM   (
  SELECT customer,
         day,
         LEAST(
           1,
           COUNT(dt) OVER ( PARTITION BY customer
                            ORDER BY day
                            RANGE BETWEEN INTERVAL '2' DAY PRECEDING
                                      AND INTERVAL '0' DAY FOLLOWING )
         ) AS has_order_in_range
  FROM   table_name t
         PARTITION BY ( customer )
         RIGHT OUTER JOIN
         ( -- Create a calendar for one month
           SELECT DATE '2017-01-01' + LEVEL - 1 AS day
           FROM   DUAL
           CONNECT BY DATE '2017-01-01' + LEVEL - 1 < ADD_MONTHS( DATE '2017-01-01', 1 )
         ) d
         ON ( t.dt = d.day )
)
GROUP BY day
ORDER BY day;