相关疑难解决方法(0)

redshift:通过窗口分区计算不同的客户

Redshift DISTINCT在其窗口函数中不支持聚合.用于COUNT声明此状态的AWS文档,并且distinct不支持任何窗口函数.

我的用例:通过不同的时间间隔和流量渠道统计客户

我希望本年度的月度和年初至今独特的客户数量,并且还按交通渠道和所有渠道的总数进行划分.由于客户可以多次访问我只需要计算不同的客户,因此Redshift窗口聚合将无济于事.

  • 我可以统计不同的客户使用count(distinct customer_id)...group by,但这只会给我四个所需的结果.
  • 并不想进入运行了一堆之间堆积每个需要计数一个完整的查询习惯union all.我希望这不是唯一的解决方案.

这就是我在postgres(或Oracle)中写的内容:

select order_month
       , traffic_channel
       , count(distinct customer_id) over(partition by order_month, traffic_channel) as customers_by_channel_and_month
       , count(distinct customer_id) over(partition by traffic_channel) as ytd_customers_by_channel
       , count(distinct customer_id) over(partition by order_month) as monthly_customers_all_channels
       , count(distinct customer_id) over() as ytd_total_customers

from orders_traffic_channels
/* otc is a table of dated transactions of customers, channels, and month of order */

where to_char(order_month, …
Run Code Online (Sandbox Code Playgroud)

sql amazon-redshift

4
推荐指数
2
解决办法
7828
查看次数

标签 统计

amazon-redshift ×1

sql ×1