2016-11-21 8 views
0

私は、国の数と公式の国の数である次の列を含む1つの列でテーブルを作成しようとしています。つまり、基本的には、2つの公用語を持つ32の国、3つの公用語を含む28の国があると言うことがあります。from句で内部結合を持つ副問い合わせを使用していますか?

これまでのところ、各国ごとの公式言語の数を数える表を作成しました。ここで

select c.name, count(l.language) number_of_languages from world.country c 
inner join (select countrycode, language, isofficial from 
world.countrylanguage where isofficial='T') l on c.code=l.countrycode group 
by c.name order by count(l.language) 

は、結果のサンプルです:

NAME             NUMBER_OF_LANGUAGES 
---------------------------------------------------- ------------------- 
Lesotho                2 
Sri Lanka                2 
Canada                 2 
Singapore                3 
Belgium                3 
Bolivia                3 
+0

質問は何ですか? –

+0

それを入手できませんでした。何が欲しいのか説明してください。 – Atul

答えて

0

まず、クエリを簡略化することができます。これは、サブクエリを使用する必要がありません:

select c.name, count(cl.language) as number_of_languages 
from world.country c inner join 
    world.countrylanguage cl 
    on c.code = cl.countrycode 
where cl.isofficial = 'T' 
group by c.name 
order by count(cl.language); 

次へ]を、サブクエリとしてこれを使用する:

select number_of_languages, count(*) 
from (select c.name, count(cl.language) as number_of_languages 
     from world.country c inner join 
      world.countrylanguage cl 
      on c.code = cl.countrycode 
     where cl.isofficial = 'T' 
     group by c.name 
    ) cl 
group by number_of_languages 
order by number_of_languages; 
関連する問題