我正在使用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必须为GROUPed或在聚合函数中使用
...所以我闭上眼睛,许个愿,然后执行以下操作(注意小组的添加):
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,以便计算转换,从而进行区分。但它确实让我对接下来应该尝试的东西感到茫然。我希望有一些简单的东西,但显然这不是我的幸运日。
关于如何使我的原始查询工作的任何想法,或关于替代方法的建议?
谢谢!
答案 0 :(得分: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;