Skip to content
Advertisement

SQL: Expression Not in GROUP BY Key

I have a transaction table t1 in Hive that looks like this:

I’m trying to compute some metrics using this table as follows:

Hive is throwing an error

SemanticException Failed to breakup Windowing invocations into Groups. At least 1 group must only depend on input columns. Expression not in GROUP BY key ‘spend’

But the column spend is being used with an aggregation function (sum), so I cannot group by this column. How can I fix this error?

Advertisement

Answer

Window functions do not need group by. You either want:

Note that the first expression can be shortened with AVG().

This brings as many rows as in the original table. If, on the other hand, you want just one row per store and zip code, then use regular aggregate functions with GROUP BY:

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement