如何计算用户 运行 的命令数?
How do I count the number of commands a user ran?
我有一个 table 看起来像这样
user_id ...
1 ...
1 ...
2 ...
我想要一个看起来像这样的table
user_id num_commands ...
1 2 ...
2 1 ...
我正在使用查询
select
user_id as id,
count(user_id) as num_commands,
...
from table_name
group by user_id
但它返回错误 FAILED: SemanticException [Error 10025]: Line 4:0 Expression not in GROUP BY key 'num_commands'
。这对我来说似乎很荒谬;我意识到 num_commands 不在 GROUP BY 语句中,但它是聚合函数的结果,那么它为什么重要?
这是错误的
select
user_id as id,
count(user_id) as num_commands
from table_name
group by user_id
count(user_id) 并按 user_id 分组引发异常
select
user_id as id,
count(user_id) as num_commands,
other_column1,
other_column2
from table_name
group by user_id
这是错误的,因为分组依据
中的列不是
没错
select
user_id as id,
count(*) as num_commands,
other_column1,
other_column2
from table_name
group by user_id, other_column1, other_column2
select
user_id as id,
count(*) as num_commands,
sum(other_column1),
avg(other_column2)
from table_name
group by user_id
我有一个 table 看起来像这样
user_id ...
1 ...
1 ...
2 ...
我想要一个看起来像这样的table
user_id num_commands ...
1 2 ...
2 1 ...
我正在使用查询
select
user_id as id,
count(user_id) as num_commands,
...
from table_name
group by user_id
但它返回错误 FAILED: SemanticException [Error 10025]: Line 4:0 Expression not in GROUP BY key 'num_commands'
。这对我来说似乎很荒谬;我意识到 num_commands 不在 GROUP BY 语句中,但它是聚合函数的结果,那么它为什么重要?
这是错误的
select
user_id as id,
count(user_id) as num_commands
from table_name
group by user_id
count(user_id) 并按 user_id 分组引发异常
select
user_id as id,
count(user_id) as num_commands,
other_column1,
other_column2
from table_name
group by user_id
这是错误的,因为分组依据
中的列不是没错
select
user_id as id,
count(*) as num_commands,
other_column1,
other_column2
from table_name
group by user_id, other_column1, other_column2
select
user_id as id,
count(*) as num_commands,
sum(other_column1),
avg(other_column2)
from table_name
group by user_id