生徒の出身国を特定しようとしていますが、データがきれいではありません。そのため、ユニオンを使用してさまざまな場所を調べますが、データがクリーンではないため、クエリがサブクエリで 2 行を返すことがあります。条件に基づいてこれらの 2 つのレコードをフィルター処理するにはどうすればよいですか。
例えば
select s.person_uid "Student ID", p.birth_date "DOB",
(
select decode(a.nation_desc,'Palestinian Territories','Gaza Strip','Great Britain','United Kingdom','Korea, Democratic People''s Rep','Democratic People''s Republic of Korea','Bahamas','The Bahamas',
'Unknown Country','Unknown','Lao People''s Democratic Republ','Lao People''s Democratic Republic','Yugoslavia','Serbia','Afganistan','Afghanistan','Ireland, Republic of (Eire)','Ireland','Iran (Islamic Republic of)','Iran, Islamic Republic of','Holy See (City Vatican State)',
'Italy','Virgin Islands','British Virgin Islands','Saint Vincent and the Grenadin','Saint Vincent and the Grenadines','England','United Kingdom',null,'Unknown',a.nation_desc)
from address a
where a.address_type = 'MA'
and a.nation_desc is not null
and address_number = 1
and a.entity_uid = p.person_uid
union
select 'Canada' nation_desc from address
where address_number = 1
and address_type = 'MA'
and nation_desc is null
and state_province in ('PE','BC','PQ','NS','QC','SK','NL','NU','AB','MB','NF','ON','NB','NT','YT')
and entity_uid = p.person_uid
union
select 'United States' nation_desc from address
where address_number = 1
and address_type = 'MA'
and nation_desc is null
and state_province in ('CA','WI','MI','NM','MA','PA','UT','DC','WA','OK','NY','SC','IA','KS','FL','OH','MN')
and entity_uid = p.person_uid
) Country
from student s
left join person p on (s.person_uid = p.person_uid)
場合によっては、学生ごとに複数の行が返され、両方の行が異なる国を示していることもあります (悪いデータ)。複数の行がある場合は、実際の国を検証するために別の列を調べて、1 行を取得する必要があります。