2016-07-02 78 views
0

我想寫這個查詢數據庫蒙迪艾爾:查詢數據庫蒙迪艾爾

  1. 列出了至少99%的島嶼組成的國家。
  2. 結果必須包括國家的名義,國家的面積,以及島嶼的總面積(提示:注意爲英國獲得的值,因爲它可能 是錯誤的指示查詢)。
  3. 編寫查詢的兩個版本。

我寫此查詢:

select c.name, c.area, ai.sum_area 
from country as c 
inner join 
    (select sum(i.area) as sum_area, gi.country 
    from island i 
    inner join geo_island gi on (i.name = gi.island) 
    group by gi.country) as ai 
on (c.code = ai.country) 
where (c.area * 0.99) <= ai.sum_area; 

但結果是錯誤的! 在一些國家,總和比全國的島嶼面積越大! 我應如何修正呢? 下面是從蒙迪艾爾數據庫的文檔:http://www.dbis.informatik.uni-goettingen.de/Mondial/ 這裏 Referential Dependencies of the Mondial Database

Country - area Country - Sum area islands  

"Guernsey";194;194  
"Iceland";103000;102829  
"Ireland";70280;84421  
"Jersey";117;117  
"Isle of Man";588;588  
"United Kingdom";244820;2622428.6  
"Bahrain";620;620  
"Brunei";5770;743122 
"Malaysia";329750;1486335  
"Christmas Island";135;135  
"Cyprus";9251;9251  
"Indonesia";1919440;10333702  
"Timor-Leste";15007;33850  
"Papua New Guinea";461690;794800  
"Japan";377835;8247391  
"Philippines";299764;1421325  
"Singapore";632.6;632.6  
"Sri Lanka";65610;65610  
"Antigua and Barbuda";442;442  
"Aruba";193;193  
"Barbados";430;430  
"Bermuda";53.3;53.3  
"Cuba";110860;1481284  
"Dominica";746;746  
"Dominican Republic";48730;76192  
"Haiti";27750;76192  
"Greenland";2175600;2175600  
"Grenada";344;344  
"Jamaica";10991;10991  
"Martinique";1128;1128  
"Montserrat";102;102  
"Curacao";444;444  
"Sint Maarten";34;87  
"Saint Martin";54;87  
"Saint Barthelemy";21;21  
"Puerto Rico";8870;8870  
"Saint Lucia";620;616  
"Trinidad and Tobago";5130;5128  
"Guam";541.3;541  
"Nauru";21;21 
"New Zealand";268680;266354  
"Niue";260;260  
"Samoa";2860;2832  
"Madagascar";587041;587041  
"Mayotte";374;374 
"Reunion";2510;2510  
"Sao Tome and Principe";1001;1001 

回答

0

嗨,你可以嘗試此查詢

SELECT c.name. c.area, x.sum_area 
FROM country c 
INNER JOIN 
(SELECT gi.country, SUM(i.area) as sum_area 
FROM geo_island gi 
INNER JOIN island i ON 
gi.island = i.name 
GROUP BY gi.country) x 
ON c.name = x.country 
WHERE (c.area * 0.99) <= x.sum_area; 
+0

查詢結果爲空 –