查找多行AVG并显示最高AVG

时间:2017-10-05 18:42:57

标签: sql database

“仅显示前2栋建筑及其相应的平均值 会话的价格。“

非常感谢任何帮助,我已经尝试了很长时间这是我迄今为止最好的尝试(我认为)......

SELECT BUILDINGNO.BUILDING, AVG(SESSIONPRICE.CONFERENCESESSION)
FROM BUILDING
JOIN CONFERENCESESSION ON BUILDINGNO.BUILDING = BUILDINGNO.CONFERENCESESSION
WHERE BUILDINGNO.BUILDING = SESSIONPRICE.CONFERENCESESSION
GROUP BY BUILDINGNO.BUILDING
Order by BUILDINGNO.BUILDING DESC;

干杯

3 个答案:

答案 0 :(得分:1)

你可以使用它。

SELECT TOP 2  BUILDINGNO.BUILDING, AVG(SESSIONPRICE.CONFERENCESESSION)
FROM BUILDING
JOIN CONFERENCESESSION ON BUILDINGNO.BUILDING = BUILDINGNO.CONFERENCESESSION
WHERE BUILDINGNO.BUILDING = SESSIONPRICE.CONFERENCESESSION
GROUP BY BUILDINGNO.BUILDING
Order by AVG(SESSIONPRICE.CONFERENCESESSION) DESC;

答案 1 :(得分:0)

使用行号/限制..取决于您使用的数据库

SELECT BUILDINGNO.BUILDING, AVG(SESSIONPRICE.CONFERENCESESSION)
FROM BUILDING
JOIN CONFERENCESESSION ON BUILDINGNO.BUILDING = 
        BUILDINGNO.CONFERENCESESSION
WHERE BUILDINGNO.BUILDING = SESSIONPRICE.CONFERENCESESSION
   AND rownum <3 /*if you are using oracle */
GROUP BY BUILDINGNO.BUILDING
Order by BUILDINGNO.BUILDING DESC;

答案 2 :(得分:0)

要解决此问题,我必须知道您使用的是哪个数据库。因为如果你使用 oracle 而不是RownumRowcount应该解决问题。如果你使用 sql server ,你应该使用{{ 1}}。

我将为Mysql提供解决方案:

TOP

对于oracle和sql server,你可以按照@Valli&amp;的答案。 @sarslan分别。

希望它有所帮助!