大数据系列 | SparkSQL&HiveSQL报错解决方法

问题原因:

问题报错提示:

Distinct window functions are not supported: count(distinct xx) windowspecdefinition(name#9, _w4#39, ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING);

在这里插入图片描述
问题报错原因:
查阅资料后发现sparksql中的窗口函数不支持COUNT(DISTINCT xxx)


问题解决:

使用size()函数配合collect_set() over()函数解决:

collect_set的作用:去重,对partition by后面的数据进行去重操作

size()的作用:使用size()函数返回字段的实现count()功能

报错修改前:
COUNT(DISTINCT totalprice)OVER(partition by name,substring(orderdate,1,7) ) totalorder
报错修改后:
SIZE(COLLECT_SET(totalprice)OVER(partition by name,substring(orderdate,1,7))) totalorder

测试结果:

在这里插入图片描述

参考:

https://blog.csdn.net/qq_41018861/article/details/117330116


版权声明:本文为woshinsy原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。