栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Clikhouse-踩坑记录之求环比

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

Clikhouse-踩坑记录之求环比

Clikhouse-踩坑记录之求环比 需求:求订单环比 问题1:后端ibatis框架不支持子查询动态筛选,所以需要在sql中就实现排序. 问题2:使用date格式的天进行聚合计算式,group by day分组会默认按照day升序,此时使用neighbor函数就不会出现乱序求环比,如果group by week/month等字符串分组,默认排序就不是我们想要的,此时使用neighbor函数就会出现乱序,环比就不对了
SELECt month                                         date,
       count(1)                                                                                           serviceNum,
       neighbor(serviceNum, -1)                                                                           serviceNumYesterday,
       round((serviceNum - serviceNumYesterday) / if(serviceNumYesterday = 0, 1, serviceNumYesterday), 3) numRatio,
       count(distinct recipientMobile)                                                                    servicePerson,
       neighbor(servicePerson, -1) as                                                                     servicePersonYesterday,
       round((servicePerson - servicePersonYesterday) / if(servicePersonYesterday = 0, 1, servicePersonYesterday),3) personRatio
FROM datacenter.tdm_order_logistics_info final
WHERe (orderState = 'COLLECTED' AND month BETWEEN '2020-09' AND '2021-09' )
GROUP BY date


如图:此时出现了很严重的乱序,有人觉得增加order by就可以了,实际order by,是在计算结果出来之后再排序,结果并不是我们想要的

SELECt toInt16(concat(substring(month,4,1),substring(month,6,2)))                                         date,
       count(1)                                                                                           serviceNum,
       neighbor(serviceNum, -1)                                                                           serviceNumYesterday,
       round((serviceNum - serviceNumYesterday) / if(serviceNumYesterday = 0, 1, serviceNumYesterday), 3) numRatio,
       count(distinct recipientMobile)                                                                    servicePerson,
       neighbor(servicePerson, -1) as                                                                     servicePersonYesterday,
       round((servicePerson - servicePersonYesterday) / if(servicePersonYesterday = 0, 1, servicePersonYesterday),3) personRatio
FROM datacenter.tdm_order_logistics_info final
WHERe (orderState = 'COLLECTED' AND month BETWEEN '2020-09' AND '2021-09' )
GROUP BY date


如图:此时乱序现象不存在了

解决:通过切割拼接函数,把月与周,转换成int值,再进行group by,就会默认按照新的分组字段默认升序,此时neighbor就不会乱序了 注意:toInt8,toInt16才会默认升序排列,toInt32,toInt64(toFloat64,toFloat32)也是无法达到想要的结果,具体为啥可能需要看源码,如有大佬知道可以留言告知,谢谢啦
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/282493.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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