[英]Calculate the average of each column in a row at the end
I'm trying to get a calculation at the end of the table that averages each column.我正在尝试在表的末尾进行计算,计算每列的平均值。 I can't seem to get my head working right on this.
我似乎无法集中精力处理这个问题。
This is my query:这是我的查询:
SELECT profiles.id, min (total_connections_average) from profiles
inner join connections ON from_profile_id = profiles.id
inner join (
select sq.from_profile_id,
count (sq.countn) as total_connections_average
from (
select from_profile_id, cast (accepted_at as date), count (*) as countn from connections
group by from_profile_id, cast (accepted_at as date)
) as sq
group by sq.from_profile_id
) sq ON sq.from_profile_id = profiles.id
where profiles.id in ('1','2','3','4','25','26')
group by profiles.id
Resulting data from the query:查询结果数据:
id ![]() |
min![]() |
---|---|
1 ![]() |
31 ![]() |
2 ![]() |
11 ![]() |
3 ![]() |
21 ![]() |
4 ![]() |
8 ![]() |
25 ![]() |
9 ![]() |
26 ![]() |
6 ![]() |
I'd like to have a row at the end with an average calculation for each column eg:我想在最后一行对每一列进行平均计算,例如:
id ![]() |
min![]() |
---|---|
1 ![]() |
31 ![]() |
2 ![]() |
11 ![]() |
3 ![]() |
21 ![]() |
4 ![]() |
8 ![]() |
25 ![]() |
9 ![]() |
26 ![]() |
6 ![]() |
10.16666667 ![]() |
14.33333333 ![]() |
Much appreciate any help.非常感谢任何帮助。
The best I can think of is a union with the previous query.我能想到的最好的是与上一个查询的联合。 This is a quick fix what I came up
这是我想到的快速解决方法
SELECT profiles.id, min (total_connections_average) min from profiles
inner join connections ON from_profile_id = profiles.id
inner join (
select sq.from_profile_id,
count (sq.countn) as total_connections_average
from (
select from_profile_id, cast (accepted_at as date), count (*) as countn from connections
group by from_profile_id, cast (accepted_at as date)
) as sq
group by sq.from_profile_id
) sq ON sq.from_profile_id = profiles.id
where profiles.id in ('1','2','3','4','25','26')
group by profiles.id
#this is union for average last column
union
select avg(agg.id), avg(agg.min)
from (
SELECT profiles.id, min (total_connections_average) min from profiles
inner join connections ON from_profile_id = profiles.id
inner join (
select sq.from_profile_id,
count (sq.countn) as total_connections_average
from (
select from_profile_id, cast (accepted_at as date), count (*) as countn from connections
group by from_profile_id, cast (accepted_at as date)
) as sq
group by sq.from_profile_id
) sq ON sq.from_profile_id = profiles.id
where profiles.id in ('1','2','3','4','25','26')
group by profiles.id
) agg
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.