2012-11-03 24 views
1

我有一個表BBC有以下的列:菜鳥關注:子查詢計算

(是指一個國家的名字在世界的特定區域內)

區域(世界大陸)

人口(在名稱名稱字段中的國家的人口) 我的問題嘗試荷蘭國際集團回答:

問題如下:
「一些國家有人口超過三倍的任何他們的鄰居(在同一地區)。給這個國家和地區的「

我想答案可能是這樣的:

SELECT a.name, a.region FROM bbc AS a 
WHERE a.region IN 
    (
     SELECT b.region FROM bbc AS b 
     GROUP By b.region 
     HAVING MIN(b.population) < 3*b.population) 

不過說實話,我失去了它的最後一行...我不知道我怎麼會覺得counteries這有任何他們在同一地區鄰國的三倍以上!相當艱難。O_O

任何和所有幫助將不勝感激。

回答

2
select 
    a.name, a.region 
from bbc as a 
where 
    a.population > 
    (
     select 3*max(b.population) 
     from bbc as b 
     where b.region = a.region and b.name <> a.name 
    ) 
+0

:)我<3你羅馬。你在哪裏獲得技能? :) – MonuMan5

+0

@ MonuMan5我有大約10年的SQL Server的經驗:)當你每天使用它時,你必須編寫高效的查詢,否則你將來無法支持它:) –

0
Select 
    a.name, 
    a.region 
From 
    bbc as a 
Where 
    Not Exists (
    Select 
     'x' 
    From 
     bbc as b 
    Where 
     a.region = b.region And 
     a.name != b.name And 
     a.population < 3 * b.population 
) 
-1
SELECT name, continent from world x 
WHERE (SELECT population from world y 
      where y.name = x.name 
      and y.continent = x.continent 
      and population > 0) > ALL 
(SELECT 3*population from world z 
      where z.continent = x.continent 
      and z.name != x.name 
      and population > 0);