gpt4 book ai didi

mysql - 获取子查询 SQL 的平均值

转载 作者:行者123 更新时间:2023-11-29 15:32:39 25 4
gpt4 key购买 nike

嗨,我有以下代码,我试图获取飞机 ID (aircraftid) 以及它们已维修的次数 (count(serviceid)),但仅在 count(serviceid) 大于时才显示它们平均计数(serviceid)。

select aircraftid, s.times
from(
select aircraftid, count(serviceid) as times
from service
group by aircraftid
) as s
having times > avg(times);

但是当我运行它时它不返回任何行

当我将代码更改为

select aircraftid, s.times
from(
select aircraftid, count(serviceid) as times
from service
group by aircraftid
) as s
having times > 2;

它返回行

最佳答案

你可以试试这个-

SELECT aircraftid, 
COUNT(serviceid) AS times
FROM service
GROUP BY aircraftid
HAVING COUNT(serviceid) > (
SELECT AVG(T) FROM
(
SELECT count(serviceid) T
FROM service
GROUP BY aircraftid
)A
)

关于mysql - 获取子查询 SQL 的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58571800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com