#成为 SQL 大师#groupby 中不能有聚合函数

背景

复制代码
select 
    t1.p_date,
    t1.member_id,
    top3_domains,
    CASE WHEN label_code = 'realtime' THEN COLLECT_SET(bayes_first) END as realtime_bys
    from 
    create_top3 t1 
    join consume_profile t2 
    on t1.p_date=t2.p_date 
    and cast(t1.member_id as string)=cast(t2.member_id as string)
    group by 1,2,3,4


报错:
aggregate functions are not allowed in GROUP BY, but found CASE WHEN (t2.`label_code` = 'realtime') THEN collect_set(t2.`bayes_first`) END;

原因&解决方案

你遇到的错误是因为在 GROUP BY 子句中,不能包含聚合函数(如 COLLECT_SET)之外的其他列或表达式。在这种情况下,你可以使用子查询来解决问题。以下是一种修改方法:

复制代码
SELECT 
    t1.p_date,
    t1.member_id,
    top3_domains,
    MAX(CASE WHEN t2.label_code = 'realtime' THEN t2.collect_set_bayes_first END) AS realtime_bys
FROM 
    create_top3 t1 
JOIN 
    (
        SELECT
            p_date,
            member_id,
            label_code,
            COLLECT_SET(bayes_first) AS collect_set_bayes_first
        FROM 
            consume_profile
        WHERE 
            label_code IN ('text_bayes_first_realtime', 'text_bayes_first_short_term', 'text_bayes_first_long_term')
        GROUP BY 
            p_date,
            member_id,
            label_code
    ) t2
ON 
    t1.p_date = t2.p_date 
    AND cast(t1.member_id as string) = cast(t2.member_id as string)
GROUP BY 
    1, 2, 3;

声明:本文章有 AI 辅助

相关推荐
Hello.Reader1 小时前
Redis 延迟监控深度指南
数据库·redis·缓存
ybq195133454311 小时前
Redis-主从复制-分布式系统
java·数据库·redis
好奇的菜鸟4 小时前
如何在IntelliJ IDEA中设置数据库连接全局共享
java·数据库·intellij-idea
tan180°4 小时前
MySQL表的操作(3)
linux·数据库·c++·vscode·后端·mysql
满昕欢喜5 小时前
SQL Server从入门到项目实践(超值版)读书笔记 20
数据库·sql·sqlserver
Hello.Reader6 小时前
Redis 延迟排查与优化全攻略
数据库·redis·缓存
简佐义的博客7 小时前
破解非模式物种GO/KEGG注释难题
开发语言·数据库·后端·oracle·golang
爬山算法7 小时前
MySQL(116)如何监控负载均衡状态?
数据库·mysql·负载均衡
JAVA学习通8 小时前
Mybatis--动态SQL
sql·tomcat·mybatis
老纪的技术唠嗑局9 小时前
OceanBase PoC 经验总结(二)—— AP 业务
数据库