根据用户交易数据计算跨类别销售-适用于进行过1次以上交易的用户

时间:2019-04-09 14:49:50

标签: sql vertica

enter image description here嗨,

输入数据

我每天都有一个用户级别的transaction_data,并且我想计算每月的跨类别销售-仅针对从一个以上类别购买的用户进行汇总。

输出-Output

有人可以建议如何在sql中进行此操作吗?

1 个答案:

答案 0 :(得分:1)

嗯。 。 。这似乎符合您的描述:

select date_trunc('month', day), category,
       sum(transaction_amount), count(distinct user_id)
from (select d.*,
             min(category) over (partition by userid, date_trunc('month', day)) as min_category,
             max(category) over (partition by userid, date_trunc('month', day)) as max_category
      from daily d
     ) d
where min_category <> max_category -- at least two categories
group by date_trunc('month', day), category;