2013-04-20 52 views
-1
select CURRENCY.currencyname, currency.currencysymbol, COUNTRYNAME.currencycode, 
     currency.exchangerate 
from CURRENCY,COUNTRYNAME 
where currency.currencycode=COUNTRYNAME.currencycode 
having currency.exchangerate >= max(currency.exchangerate) 
group by CURRENCY.currencyname, currency.currencysymbol, 
     COUNTRYNAME.currencycode, currency.exchangerate; 

這是我得到的輸出,我知道是錯誤的,因爲我試圖只顯示最大的匯率將是。所以任何幫助解決這個查詢將非常感激。我的Oracle SQL Developer錯誤的查詢輸出時使用where和具有相同的SQL語句

CURRENCYNAME   CURRENCYSYMBOL  CURRENCYCODE EXCHANGERATE 
-------------------- -------------------- ------------ ------------ 
British Pound  £     GBP      1, 
US Dollar   $     USD    1.9626 , 
Danish Krone   kr     DKK    9.9918 , 
Malaysian Ringgit RM     MYR    6.35392 , 
Euro     €     EUR    1.34076 , 
Indian Rupee   Rs     INR    77.3265 , 
Japanese Yen   ¥     JPY    209.881, 
+0

什麼是你應該做的? – 2013-04-20 10:55:36

+0

與_trying顯示_它正在做什麼它在選擇.. – dbf 2013-04-20 10:56:17

+0

選擇'max(currency.exchangerate)'怎麼樣? – 2013-04-20 11:01:35

回答

0

請嘗試以下

SELECT CURRENCY.currencyname, currency.currencysymbol, 
     COUNTRYNAME.currencycode, MAX(currency.exchangerate) as ExchangeRate 
FROM CURRENCY 
INNER JOIN COUNTRYNAME ON COUNTRYNAME.currencycode = currency.currencycode 
GROUP BY CURRENCY.currencyname, currency.currencysymbol, COUNTRYNAME.currencycode; 
0

關於您的查詢......

1。您無法將MAX(聚合)值與同一查詢級別的基準值進行比較。您可以使用派生表(也稱爲「子查詢」),例如

 
    select c.currencyname, c.currencysymbol, n.currencycode, c.exchangerate 
    from CURRENCY c 
    join COUNTRYNAME n on c.currencycode=n.currencycode 
    where c.exchangerate = (select max(exchangerate) from currency) 

2。從上面你會注意到我使用了JOIN來連接這兩個表。大約21年前,隨着ANSI-92連接語法的出現,使用表名的逗號列表變得不合時宜。

3。顯示的查詢無法工作。 GROUP BY子句屬於HAVING子句之前,而不是之後。話雖如此,上面的第一點適用,無論如何您都無法將MAX值與(基準)列值進行比較。

+0

感謝隊友完美對不起我推出了逗號列表只是一個打字錯誤 – 2013-04-20 11:31:16

0

該查詢可能可以簡化爲:

select max(currencyname), max(currencysymbol), currencycode, max(exchangerate) 
from currency group by currencycode 

( - 假設沒有那些從未使用過任何國家的貨幣)