【问题标题】:Analytic function to count each customer with many accounts only once分析功能,只计算一次拥有多个帐户的每个客户
【发布时间】:2019-08-22 23:25:49
【问题描述】:

我今天过得很慢,出于某种原因,我对如何做到这一点感到茫然。请帮我。 我有一个分析功能,可以只计算每个拥有多个帐户的客户一次。想要每个不同的客户共。我必须列出客户及其帐户。每个客户的多个帐户

我正在使用这样的功能。 COUNT(1) OVER(按 cust.SK_CUST_RM_ID 分区) total_cnt1

我正在寻找这个输出

cust account  distinc_cust
 1     a           3
 1     b           3
 2     a           3
 2     b           3
 2     c           3
 3     a           3

【问题讨论】:

    标签: sql netezza analytic-functions


    【解决方案1】:

    我认为 Netezza 不支持将 count(distinct) 作为分析函数。所以,你可以使用这个技巧:

    select t.*,
           max(dr) over () as distinct_cust
    from (select t.*, dense_rank() over (order by cust) as dr
          from t
         ) t;
    

    作为分析函数,您只需使用:

    select t.*, count(distinct cust) over ()
    from t;
    

    【讨论】:

    • 确实如此,那么我该如何使用count distinct?
    猜你喜欢
    • 1970-01-01
    • 2010-12-25
    • 1970-01-01
    • 2016-06-11
    • 2018-08-17
    • 1970-01-01
    • 2020-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多