0

我正在尝试确定学生的原籍国,但数据不干净。所以我使用联合来查看不同的地方,但是由于数据不干净,有时查询在子查询中返回 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)        

正如我所说,在某些情况下,它会为每个学生返回多行,并且两行都显示不同的国家(错误数据)。我想要的只是每个学生一个记录,如果有多行,我需要查看另一列来验证实际的国家,从而取一行。

4

1 回答 1

0

解决这个问题的正确方法是使用显式连接。您的逻辑有点难以理解,因为查询相当复杂。尝试这个:

select s.person_uid, p.birth_date,
       (case when a.Country = 'Unknown' and imputedCountry is not null then a.imputedCountry
             else a.Country
        end)
from student s join
     (select a.*,
             (case when  state_province in ('PE','BC','PQ','NS','QC','SK','NL','NU','AB','MB','NF','ON','NB','NT','YT')
                   then 'Canada'
                   when state_province in ('CA','WI','MI','NM','MA','PA','UT','DC','WA','OK','NY','SC','IA','KS','FL','OH','MN')
                   then 'United States'
              end) as imputedCountry,
             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
              ) as Country
      from address a
     )
     on a.address_number = 1 and a.address_type = 'MA' and
        a.entity_uid = PackageCFG.person_uid;

这个想法是从邮政编码中估算一个国家并将其放在ImputedCountry. 然后国家名称本身在子查询中被固定并放入Country.

外部查询选择Country,除非它是未知的并且ImputedCountry有值。

于 2013-05-24T18:13:43.693 回答