我尝试使用查找表查询表,这是因为这些表具有相同值的不同名称/代码。
例如,Employee
和Production
表格具有相同的Site
字段,但它们的命名方式不同。所以我希望来自{{1}的字段Person
和来自Employee
的{{1}},此加入是通过相同的Percent
完成的。
Production
所需输出:
Site
已尝试过以下方面:
=Employee=
id | Person | Site
---+----------+------
1 | Joe | Mex
2 | Sally | MXN
3 | Eric | Mex
4 | John | USA
=Production=
id | % | Site
---+----------+------
1 | 98 | Mexico
2 | 97 | USA
3 | 98 | India
4 | 96 | Thailand
=SiteCode=
id | Site | Sitecode
---+----------+------
1 | Mexico | Mex
2 | Mexico | MXN
3 | USA | USA
4 | USA | Unite States of America
任何帮助都会受到赞赏,只是无法绕过它...
答案 0 :(得分:1)
select Employee.Person, Production.Percent
from
Employee
INNER JOIN SiteCode on Employee.Site = SiteCode.Sitecode
INNER JOIN Production on SiteCode.Site = Production.Site
答案 1 :(得分:1)
这很棘手。你应该真正修复数据。但是,你可以这样做:
with sc as (
select sc.*,
(case when sitecode = 'Unite States of America' then 'USA'
else sitecode
end) as new_sitecode
from sitecode sc
)
select e.Person, p.percent
from employee e join
sc
on e.site = sc.new_sitecode join
production p
on p.site = sc.site;