栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

2016年的博客文章指出了这个问题,并提供了一个基本的解决方法,因此谢谢Mark D.
Adams。奇怪的是,我在所有的网络上都找不到,因此我正在共享我的(经过测试的)解决方案。

关键的见解是

dense_rank()
,按相关商品排序,可以为相同商品提供相同的排名,因此,最高排名也是唯一商品的计数。如果您尝试为我想要的每个分区交换以下内容,那就太糟了:

dense_rank() over(partition by order_month, traffic_channel order by customer_id)

由于您需要最高的排名,因此您必须对所有内容进行子查询,然后从每个获得的排名中选择最大值。 重要的是将外部查询中的分区与子查询中的相应分区进行匹配。

select distinct       order_month       , traffic_channel       , max(tc_mth_rnk) over(partition by order_month, traffic_channel) customers_by_channel_and_month       , max(tc_rnk) over(partition by traffic_channel)  ytd_customers_by_channel       , max(mth_rnk) over(partition by order_month)  monthly_customers_all_channels       , max(cust_rnk) over()  ytd_total_customersfrom (       select order_month   , traffic_channel   , dense_rank() over(partition by order_month, traffic_channel order by customer_id)  tc_mth_rnk   , dense_rank() over(partition by traffic_channel order by customer_id)  tc_rnk   , dense_rank() over(partition by order_month order by customer_id)  mth_rnk   , dense_rank() over(order by customer_id)  cust_rnk       from orders_traffic_channels       where to_char(order_month, 'YYYY') = '2017'     )order by order_month, traffic_channel;

笔记

  • max()
    dense_rank()
    必须匹配的分区
  • dense_rank()
    将对null值进行排名(所有排名都在同一排名,即最大值)。如果您不希望对
    null
    值进行计数,则需要一个
    case when customer_id is not null then dense_rank() ...etc...
    ,或者,
    max()
    如果您知道存在空值,则可以从中减去一个。


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/441487.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号