0

以下是我的原始数据

DATE        Product_id Customer_ID
----------------------------
01-JUL-14       60      A
01-AUG-14       45      A
01-SEP-14       45      A
01-SEP-14       50      A
01-OCT-14       30      A
01-JUL-14       60      B
01-AUG-14       45      B
01-SEP-14       45      B
01-OCT-14       30      B

这是我滚动计数的理想结果

MMYY     Distinct   Customer
         Product
-------------------------
JUL-14      1       A
AUG-14      2       A
SEP-14      3       A
OCT-14      3       A
JUL-14      1       B
AUG-14      2       B
SEP-14      2       B
OCT-14      2       B

我需要它的工作方式是,对于每个 MMYY,我需要回顾 3 个月,并且可以重复 COUNT DISTINCT Products per CUSTOMER Products。而且一个客户在同一个月内可以拥有超过 1 个产品。

通常我会这样写查询

SELECT
  customer_ID, 
  T.Date as MMYY,
  COUNT(DISTINCT Product_id)
    OVER (PARTITION BY customer_ID ORDER BY T.Date ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)
    AS Last_3_month_count
    FROM T

但问题是我们不能使用COUNT(DISTINCT Product_id)它给出一个错误。建议我用其他方法来解决这个问题。

4

2 回答 2

0

我找到了我的解决方案这给了我与我想要的完全相同的结果集。

select to_char(a.date,'MM-YY') as mm_yy
          ,count(distinct b.product_id)
          ,b.customer_id
      from raw_data a
      join raw_data b
        on a.customer_id=b.customer_id
       and b.date between(add_months(a.date,-2) and a.date)
  group by to_char(a.date,'MM-YY')
          ,b.customer_id
于 2019-12-13T16:26:11.270 回答
0

更新答案

您可以按如下方式运行查询

    select to_char(b.date,'MM-YY') as mm_yy
          ,count(distinct b.product_id)
          ,b.customer_id
      from raw_data a
      join raw_data b
        on a.customer_id=b.customer_id
       and b.date>=add_months(a.date,-2)
       and b.date<=a.date
  group by to_char(b.date,'MM-YY')
          ,b.customer_id

如果 vertica 允许在选择中进行选择,则可以使用

select to_char(b.date,'MM-YY') as mm_yy
      ,(select count(distinct product_id)
          from raw_data a
         where a.customer_id=b.customer_id
           and a.date>=add_months(b.date,-3)
           and a.date<b.date) as cnt_distinct_pid
       ,b.customer
  from raw_data b
于 2019-12-12T15:50:52.410 回答