V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
isStatic
V2EX  ›  数据库

XDM,问一个 clickhouse 的 group by column_name with rollup 查询问题

  •  
  •   isStatic · 193 天前 · 338 次点击
    这是一个创建于 193 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我有个查询是使用了 with rollup ,具体类似这样:

    select column_1,if(column_2 is NULL or column_2 ='' ,'all',column_2) as column_2
    from (
    select column_1, if(column_2 is NULL or column_2 ='' ,'type1',column_2) as column_2
    from table 
    ) t1 --这里省略几个 left join
    group by column_1,column_2 with rollup
    

    类似这样想要把汇总行的 column_2 字段(字符串类型)变成 all ,但是实际上执行这个 sql 没办法替换汇总行的 column_2 字段。 我目前的做法是在这个 sql 上再包一层,在上层匹配汇总行。问题是我原本的 sql 哪里写错了,是因为 with rollup 的执行顺序问题吗?

    1 条回复
    isStatic
        1
    isStatic  
    OP
       192 天前
    查看了一下执行计划,发现 Rollup 的 key 是 if(column_2 is NULL or column_2 ='' ,'all',column_2)这种形式的。而如果在分组的时候加个表名前缀 t1.column_1,t1.column_2 ,才是以 column_1,column_2 作为 key 分组执行 rollup 。为什么会这样?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   970 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 22:51 · PVG 06:51 · LAX 15:51 · JFK 18:51
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.