我正在编写一个hive查询,其中我需要按几个字段进行分组,但是我需要选择除group by语句之外的其他字段。也就是说,
IMessage
HIVE抱怨并说select A,B,C from table_name GROUP BY A,B
。它要求我将Invalid table alias or column reference C
放在GROUP BY部分,但这会改变我的逻辑。我该如何解决这个问题?
答案 0 :(得分:1)
HIVE-Select-statement-and-group-by-clause - group by必须与一些聚合函数一起使用,如count,sum等。
因此,必须在列C
示例:
select A,B,count(C) as Total_C from table_name GROUP BY A,B;
select A,B,SUM(C) as Total_C from table_name GROUP BY A,B;
答案 1 :(得分:0)
您可以尝试使用cluster by
代替group by
select A,B,C from table_name CLUSTER BY A,B
答案 2 :(得分:-1)
您必须在分组后加入。
select T1.*, t2.c from (select a,b, count(*) as SomeAggFunc from table group by a,b) T1;
<join condition> table t2 on t1.a=t2.a and t1.b=t2.b