• 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

  • 相关阅读:
    [luogu p4447] [AHOI2018初中组]分组
    咕咕咕通知
    [luogu p3817] 小A的糖果
    [luogu p1228] 地毯填补问题
    [luogu p1259] 黑白棋子的移动
    [luogu p3612] [USACO17JAN]Secret Cow Code S
    [luogu p1990] 覆盖墙壁
    [luogu p1928] 外星密码
    [luogu p2036] Perket
    [luogu p2392] kkksc03考前临时抱佛脚
  • 原文地址:https://www.cnblogs.com/thxj/p/12742850.html
Copyright © 2020-2023  润新知