MySQL ( v5.7.30 ) 查询 AVG 的 AVG

Posted

技术标签:

【中文标题】MySQL ( v5.7.30 ) 查询 AVG 的 AVG【英文标题】:MySQL ( v5.7.30 ) query AVG of AVGs 【发布时间】:2020-09-03 13:15:23 【问题描述】:

所以我有这两张桌子:

工作:

-------------------------------------------------
id        business_id        other_columns
-------------------------------------------------
1         223                 xxxxxx
-------------------------------------------------
1         12                  xxxxxx
-------------------------------------------------

businesses_ratings:

--------------------------------------------------------------------------------------
id   business_id   professional   communication   safety   respectful   dependability        
--------------------------------------------------------------------------------------
1    223           4              2               5        4            3      
--------------------------------------------------------------------------------------
2    223           3              5               2        4            5
--------------------------------------------------------------------------------------
3    223           1              2               5        4            4
-------------------------------------------------------------------------------------- 

我想选择特定 business_id 的工作,并将该 business_id 的总体评分附加到每个工作,计算为 AVG((AVG(专业), AVG(通信), AVG(安全), AVG(尊重) , AVG(可靠性))

我可以在一个查询中实现这一点吗?

LE:我将在此处附加我尝试过的查询(还包含 WHERE 子句,也许它有助于更​​好地解释我需要实现的目标。还有抛出的错误:

SELECT * FROM jobs 
CROSS JOIN (
    SELECT count(*) totalJobs FROM jobs 
    WHERE (
        ( JSON_CONTAINS(skills, '"id":1,"val":"Carpenter"') ) 
        AND  NOT JSON_CONTAINS(workers, '"id":6,"fullname":"Cip"') 
        AND NOT JSON_CONTAINS(applicants, '"id":6,"fullname":"Cip"')
    )
) ttl
CROSS JOIN (
    SELECT AVG(
        (SELECT AVG(professional) FROM businesses_ratings WHERE business_id=jobs.business_id) + 
        (SELECT AVG(communication) FROM businesses_ratings WHERE business_id=jobs.business_id) + 
        (SELECT AVG(safety) FROM businesses_ratings WHERE business_id=jobs.business_id) + 
        (SELECT AVG(respectful) FROM businesses_ratings WHERE business_id=jobs.business_id) + 
        (SELECT AVG(dependability) FROM businesses_ratings WHERE business_id=jobs.business_id)
    ) business_rating FROM businesses_ratings WHERE business_id=jobs.business_id
) avg
WHERE (
    ( JSON_CONTAINS(skills, '"id":1,"val":"Carpenter"') ) 
    AND  NOT JSON_CONTAINS(workers, '"id":6,"fullname":"Cip"') 
    AND NOT JSON_CONTAINS(applicants, '"id":6,"fullname":"Cip"')
) 
ORDER BY start_date LIMIT 3

和错误:

Unknown column 'jobs.business_id' in 'where clause'

【问题讨论】:

平均值为总和/计数:(AVG(professional)+AVG(communication)+AVG(safety)+AVG(respectful)+AVG(dependability)/5。或sum(professional+communication+safety+respectful+dependability)/5*count(*) 【参考方案1】:

我认为你想要聚合和窗口函数:

select 
    business_id, 
    rank() over(
        order by avg(professional) + avg(communication) + avg(safety)
    ) as rn
from businesses_ratings
group by business_id

您可以根据需要使用附加列扩展窗口函数的order by 子句。

我对按 3 个平均值的平均值进行排名的兴趣持怀疑态度 - 但上述查询似乎是对您要求的合理解释。

在早期版本中,一个选项使用用户定义的变量来计算排名:

select t.*, @rn := @rn + 1 rn
from (
    select 
        business_id, 
        avg(professional) + avg(communication) + avg(safety) sum_avg
    from businesses_ratings
    group by business_id
    order by sum_avg
) t
cross join (select @rn := 0) x

【讨论】:

感谢@gmb,但我需要从工作中选择 * 并将 business_id 的总体评分附加到每个结果中。运行您的查询,返回此错误:Error in query (1064): Syntax error near '( order by avg(professional) + avg(communication) + avg(safety) ) ' at line 3 @xyboox:你运行的是哪个版本的 mysql?窗口函数仅在 MySQL 8.0 中可用。 运行 MySQL v5.7.30 感谢 GMB,您的解决方案在进行一些非常小的调整后也能正常工作。我接受了@gordon-linoff 的回答,只是因为它出现在您的回答之前,并且“开箱即用”。【参考方案2】:

您的查询似乎比必要的复杂得多。我想你想要:

select br.business_id,
       avg(professional),
       avg(communication),
       avg(safety),
       avg(respectful),
       avg(dependability),
       (avg(professional) + avg(communication) + avg(safety) + avg(respectful) + avg(dependability)) / 5 as overall_avg
from businesses_ratings br
group by br.business_id;

【讨论】:

谢谢,将您的选择插入我的 CROSS JOIN 并按照我需要的方式工作。

以上是关于MySQL ( v5.7.30 ) 查询 AVG 的 AVG的主要内容,如果未能解决你的问题,请参考以下文章

使用 avg 和 group by 的 SQL 查询

在 Django 中使用 mysql 查询或等效项

mysql有意思的order by 子查询

别名和聚合函数的 MySQL 查询问题

SQL 将 COUNT 和 AVG 查询与 SELECT 结合起来

在 MySQL 中与 MAX、MIN 和 AVG 函数一起使用 MEDIAN