我相信这对你们大多数人来说都是一个非常简单的问题,但它让我疯狂...... 我有一个这样的表(简化):
| customer_id | date | purchase amount |
我需要每天提取当天购买的客户数量,以及在当前购买前30天内至少进行购买的客户数量。
我尝试使用这样的子查询:
select purch_date as date, count (distinct customer_id) as DAU,
count(distinct (select customer_id from table where purch_date<= date and purch_date>date-30)) as MAU
from table
group by purch_date
Netezza返回错误,指出不支持子查询,我应该考虑重写查询。但是如何?!?!?
我尝试使用case when
语句,但没有用。事实上,以下内容:
select purch_date as date, count (distinct customer_id) as DAU,
count(distinct case when (purch_date<= date and purch_date>date-30) then player_id else null end) as MAU
from table
group by purch_date
没有返回错误,但是MAU和DAU列是相同的(这是错误的)。 请问有人帮帮我吗?非常感谢
答案 0 :(得分:1)
我终于明白了:)对于所有感兴趣的人,这是我解决它的方式:
select a.date_dt, max(a.dau), count(distinct b.player_id)
from (select dt.cal_day_dt as date_dt,
count(distinct s.player_id) as dau
FROM IA_PLAYER_SALES_HOURLY s
join IA_DATES dt on dt.date_key = s.date_key
group by dt.cal_day_dt
order by dt.cal_day_dt
) a
join (
select dt.cal_day_dt as date_dt,
s.player_id as player_id
FROM IA_PLAYER_SALES_HOURLY s
join IA_DATES dt on dt.date_key = s.date_key
order by dt.cal_day_dt
) b on b.date_dt <= a.date_dt and b.date_dt > a.date_dt - 30
group by a.date_dt
order by a.date_dt;
希望这有用。
答案 1 :(得分:0)
我不相信netezza支持选择行中的子查询...转到from语句
select pur_date as date, count(distinct customer_id) as DAU
from table
group by purch_date
select pur_date as date, count (distinct customer_ID) as MAU
from table
where purch_date<= date and purch_date>date-30
group by purch_date
我希望这对于MAU和DAU是正确的。加入他们以获得结果:
select a.date, a.dau, b.mau
from
(select pur_date as date, count(distinct customer_id) as DAU
from table
group by purch_date) a
left join
(select pur_date as date, count (distinct customer_ID) as MAU
from table
where purch_date<= date and purch_date>date-30
group by purch_date) b
on b.date = a.date