我似乎很难解决什么问题。数据如下表所示
表格1num_users | time_taken
----------------------
10.1 | 11.2
10.1 | 11.5
10.2 | 11.1
10.4 | 11.9
11.01 | 12.2
11.1 | 11.8
11.7 | 13.2
11.72 | 13.1
我想计算平均值。每个time_taken
值的num_users
四舍五入为0十进制。这是我尝试过的。
select
round(num_users,0) as num_users
, round(avg(time_taken),1) as time_taken
from table1 as a
group by a.num_users
order by a.num_users
然而,这为每个不同的num_users
值提供了多行(四舍五入为0十进制) - 这里有任何想法吗?
num_users | time_taken
----------------------
10 | 11.4
11 | 12.0
12 | 13.2
答案 0 :(得分:2)
在group by
:
select round(num_users, 0) as num_users,
round(avg(time_taken), 1) as time_taken
from table1 as a
group by round(num_users, 0)
order by round(num_users, 0);
我说要重复它,因为表达式的名称与列的名称相同。或者,你可以写:
select round(num_users, 0) as new_num_users,
round(avg(time_taken), 1) as time_taken
from table1 as a
group by new_num_users
order by new_num_users;
甚至:
group by 1
order by 1
即使您从group by a.num_users
中删除限定符,Postgres仍会将其解释为列名而不是新别名。