gpt4 book ai didi

sql - 为什么 AVG() 没有给出结果?

转载 作者:行者123 更新时间:2023-12-02 01:21:51 24 4
gpt4 key购买 nike

我尝试只选择体重高于所有狗的平均体重的狗。

我有这个 SQL 查询:

SELECT nameAni, weigth
FROM Animal
WHERE idTpAni IN (SELECT idTpAni
FROM TypeAnimal
WHERE libTpAni = 'DOG')
GROUP BY nameAni
HAVING AVG(weigth) < weigth;

如果我给小费 30 而不是 AVG(weigth),我会得到我想要的结果。

你能解释一下为什么我在输入“AVG(...)”时没有结果吗?

最佳答案

SELECT nameAni, weigth
FROM Animal
WHERE idTpAni IN (SELECT idTpAni
FROM TypeAnimal
WHERE Lower(libTpAni) = 'dog')
AND weigth > (SELECT Avg(weigth)
FROM Animal
WHERE idTpAni IN (SELECT idTpAni
FROM TypeAnimal
WHERE Lower(libTpAni) = 'dog'))

如果想走的简单一点,引用下面

    DECLARE @weight FLOAT; --mind the type of this variable

SELECT @weight=Avg(weigth)
FROM Animal
WHERE idTpAni IN (SELECT idTpAni
FROM TypeAnimal
WHERE Lower(libTpAni) = 'dog')

SELECT nameAni, weigth
FROM Animal
WHERE idTpAni IN (SELECT idTpAni
FROM TypeAnimal
WHERE Lower(libTpAni) = 'dog')
AND weigth > @weight

关于sql - 为什么 AVG() 没有给出结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39659447/

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