Alibaba MaxCompute 如何根据键对值进行分组?
How to group values based on key in Alibaba MaxCompute?
我在尝试根据 Alibaba MaxCompute 中的键对值进行分组时遇到问题。我之前在阿里云MaxCompute中使用过类似的查询,但现在它抛出一个错误。
例如,
SELECT * FROM table GROUP BY key;
它应该可以工作,但它会抛出像
这样的错误
FAILED: ODPS-0130071:[1,8] Semantic analysis exception - column
reference table.value should appear in GROUP BY key
任何人都可以帮助我理解为什么会出现此错误。
MaxCompute早期版本支持select * from group by key
即使匹配*的列不在GROUP BY key中。兼容Hive,MaxCompute 2.0禁止使用这种语法,除非GROUP BY列表是所有源表中的列
示例:
Scenario 1: The GROUP BY key does not include all columns.
语法不正确:
SELECT * FROM t GROUP BY key;
正确的语法:
SELECT DISTINCT key FROM t;
Scenario 2: The GROUP BY key includes all columns.
不推荐的语法:
SELECT * FROM t GROUP BY key, value; -- t has columns key and value
推荐语法:
SELECT DISTINCT key, value FROM t;
我在尝试根据 Alibaba MaxCompute 中的键对值进行分组时遇到问题。我之前在阿里云MaxCompute中使用过类似的查询,但现在它抛出一个错误。
例如,
SELECT * FROM table GROUP BY key;
它应该可以工作,但它会抛出像
这样的错误FAILED: ODPS-0130071:[1,8] Semantic analysis exception - column reference table.value should appear in GROUP BY key
任何人都可以帮助我理解为什么会出现此错误。
MaxCompute早期版本支持select * from group by key
即使匹配*的列不在GROUP BY key中。兼容Hive,MaxCompute 2.0禁止使用这种语法,除非GROUP BY列表是所有源表中的列
示例:
Scenario 1: The GROUP BY key does not include all columns.
语法不正确:
SELECT * FROM t GROUP BY key;
正确的语法:
SELECT DISTINCT key FROM t;
Scenario 2: The GROUP BY key includes all columns.
不推荐的语法:
SELECT * FROM t GROUP BY key, value; -- t has columns key and value
推荐语法:
SELECT DISTINCT key, value FROM t;