2017-10-05 68 views
1

「僅顯示前兩個建築物及其對應的平均價格。」查找多行並顯示最高AVG的AVG

任何幫助是極大的讚賞,我一直在努力的年齡,這是我最好的嘗試(我認爲),那麼遠,

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; 

乾杯

+0

您使用的是哪個數據庫? –

+1

如果以下任何答案對您有幫助或解決了您的問題,請注意並/或接受答案。這對未來的訪問者會有幫助。 –

回答

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; 
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; 
+0

我剛剛從您的查詢中複製。 CONFERENCESESSION是表格中的一列嗎?正在建造一個桌子? – Valli

0

要解決我必須知道哪些數據庫您正在使用的問題。對於也就是說,如果你正在使用甲骨文RownumRowcount應該解決的問題。如果您正在使用SQL服務器,你應該使用TOP

我給MySQL的解決方案:

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 AVG(SESSIONPRICE.CONFERENCESESSION) DESC 
limit 2; 

對於Oracle和SQL Server,你可以分別執行@Valli & @sarslan的答案。

希望它有幫助!