我正在尝试确定学生的原籍国,但数据不干净。所以我使用联合来查看不同的地方,但是由于数据不干净,有时查询在子查询中返回 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)
正如我所说,在某些情况下,它会为每个学生返回多行,并且两行都显示不同的国家(错误数据)。我想要的只是每个学生一个记录,如果有多行,我需要查看另一列来验证实际的国家,从而取一行。