1

我正在使用 Netezza。我正在处理营销数据,特别是优惠券。现在我正在计算不同的每日优惠券兑换者;没什么大不了。不过,我想知道迄今为止不同的救赎者的数量。请注意,这不仅仅是每日赎回者的总和,因为客户可能在不同的日子赎回,因此每日赎回者的总和可能是多次计算的客户。

我闭上眼睛,许了一个愿望,然后执行了以下查询,希望它能起作用:

select redemption_date
      ,count(distinct(customer_id)) as day_redeemers
      ,count(distinct(customer_id)) over (partition by null rows unbounded preceding) as cml_redeemers
from coupon_history
group by 1
order by 1

但 Netezza 抱怨:错误 [HY000] 错误:属性 CUSTOMER_ID 必须被分组或在聚合函数中使用

...所以我闭上眼睛,许愿,然后执行以下操作(注意添加到 group by):

select redemption_date
      ,count(distinct(customer_id)) as day_redeemers
      ,count(distinct(customer_id)) over (partition by null rows unbounded preceding) as cml_redeemers
from coupon_history
group by 1,customer_id
order by 1

Netezza 抱怨如下:

ERROR [HY000] ERROR:  DISTINCT aggregate not allowed in window with ORDER BY or frame specification

该错误使我认为 Netezza 在内部订购了 customer_id 以计算转换并因此计算差异。但这确实让我对接下来应该尝试的事情感到不知所措。我希望有一些简单的东西,但显然这不是我的幸运日。

关于如何使我的原始查询工作的任何想法,或关于替代方法的建议?

谢谢!

4

1 回答 1

3

您总是可以诉诸蛮力 - 即相关子查询:

select redemption_date,
       count(distinct(customer_id)) as day_redeemers,
       (select count(distinct ch2.customer_id)
        from coupon_history ch2
        where ch2.redemption_date <= ch.redemption_date
       ) as cml_redeemers
from coupon_history ch
group by 1
order by 1;

当然,性能不会那么好。

编辑:

解决此问题的另一种方法是获取每个客户的第一个兑换日期,然后只使用累积金额:

select minrd,
       sum(count(*)) over (order by minrd) as cml_redeemers
from (select ch.customer_id, min(redemption_date) as minrd
      from coupon_history ch
      group by ch.customer_id
     ) ch
group by minrd;
于 2014-12-12T01:27:00.767 回答