简体   繁体   English

最后计算一行中每一列的平均值

[英]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 ID min分钟
1 1个 31 31
2 2个 11 11
3 3个 21 21
4 4个 8 8个
25 25 9 9
26 26 6 6个

I'd like to have a row at the end with an average calculation for each column eg:我想在最后一行对每一列进行平均计算,例如:

id ID min分钟
1 1个 31 31
2 2个 11 11
3 3个 21 21
4 4个 8 8个
25 25 9 9
26 26 6 6个
10.16666667 10.16666667 14.33333333 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM