Select sg_gameno, Max(sg_Year), sg_end, sg_hostcity, country_olympic_name
from Summergames s, Country co
where s.country_isocode = co.country_isocode
これの何が悪いのかわからない。私は最後の年を取得したい。MAXか何か他のものを使うべきですか?
1 つの列 ( ) を集計し、他の列を集計しない場合は、句sg_year
が必要です。GROUP BY
Select sg_gameno, Max(sg_Year), sg_end, sg_hostcity, country_olympic_name
from Summergames s,
Country co
where s.country_isocode = co.country_isocode
group by sg_gameno, sg_end, sg_hostcity, country_olympic_name
は構文的に有効です。必要な結果が得られるかどうかは別の問題です。テーブルがどのように見えるか、どのようなデータが含まれているか、どのような結果が必要かなどをお知らせください。
Oracle では、個々の列が GROUP BY 句に含まれていない限り、SELECT リストに集計関数と個々の列を含めることはできません。
RANK または DENSE_RANK 関数を使用して、年に基づいてレコードをランク付けし、結果セットから上位ランクの行を選択できます。
select * from (
select sg_gameno, sg_Year, sg_end, sg_hostcity, country_olympic_name,
rank() over (order by sg_year desc) as ranking
from Summergames s, Country co
where s.country_isocode = co.country_isocode
)
where ranking = 1;
次のクエリを使用して同じ結果を取得することもできます。最適なパフォーマンスを発揮するものを選択する必要があります。
select sg_gameno, sg_Year, sg_end, sg_hostcity, country_olympic_name
from Summergames s, Country co
where s.country_isocode = co.country_isocode
and sg_Year = (select max(sg_Year)
from Summergames s, Country co
where s.country_isocode = co.country_isocode);