zoukankan      html  css  js  c++  java
  • Clickhouse 开窗函数

    向大神学习:https://mp.weixin.qq.com/s/ubahKb5NajqHv0LGvAP-iw

    SELECT arrayEnumerate([10,20,30,10,40]) AS row_number, /*按照数据顺序输出应的ID*/
    arrayEnumerateDense([10,20,30,10,40]) AS dense_rank, /*按照数据出现输出ID,其中数字相同的,ID 也会相同*/
    arrayEnumerateUniq([10,20,30,10,40]) AS uniq_ran ; /*输出数据出现次数*/



    CREATE TABLE test_data engine = Memory AS
    WITH(
    SELECT ['A','A','A','A','B','B','B','B','B','A','59','90','80','80','65','75','78','88','99','70']
    )AS dict;
    select * from test_data ;

    /*第一步:按val 排序,因为条件是 order by val,按照分时升序排列*/
    SELECT * FROM test_data ORDER BY val;


    /*第二步:按id 分组,因为条件是PARTITION BY id*/
    select id
    from
    (
    select * from test_data order by val as asc
    )
    group by id ;

    /*第三步,计算val的RANK ,需要用到刚介绍的几个
    arrayEnumerate*函数,由于它们的入参要求数组,
    所以先使用groupArray将val转成数组:*/
    select
    id,
    groupArray(val) as arr_val,
    arrayEnumerate(arr_val) as row_number,
    arrayEnumerateDense(arr_val) as dense_rank,
    arrayEnumerateUniq(arr_val) as uniq_rank
    from
    (
    select *
    from test_data
    order by val asc
    ) group by id;

    /*第四步:数组展开,利用ARRAY JOIN 将数组展开,并按照id,RANK 列排序*/
    select
    id,
    val,
    row_number,
    dense_rank,
    uniq_rank
    from
    (
    select
    id,
    groupArray(val) as arr_val,
    arrayEnumerate(arr_val) as row_number,
    arrayEnumerateDense(arr_val) as dense_rank,
    arrayEnumerateUniq(arr_val) as uniq_rank
    from
    (
    select * from test_data
    order by val asc
    )
    group by id
    )
    ARRAY JOIN
    arr_val as val,
    row_number,
    dense_rank,
    uniq_rank
    order by
    id asc,
    row_number asc,
    dense_rank asc

  • 相关阅读:
    URL
    VI,CI,UI
    ubuntu优化使用
    Django入门之自定义页面
    python3 连接SQLserver
    Python3 捕捉异常
    python3 异常处理
    Django入门
    较大型站立会议(交付前两天)--张永组-2014-04-15
    站立会议-2014-04-14
  • 原文地址:https://www.cnblogs.com/thxj/p/12742850.html
Copyright © 2011-2022 走看看