我有两张桌子:
table AV, KEY AVNR //(this is the request)
table AVKW, KEY AVNR and KWCD //(these are the required qualifications for the request, N:1 to AV)
和:
table EM, KEY EMNR //(this is the employee)
table EMKW, KEY EMNR and KWCD //(these are the qualifications of the employee, N:1 to EM)
样本数据:
AV:
AVNR (descr)
10 job december 10th
20 job december 11th
30 job december 12th
AVKW:
AVNR KWCD
10 operator
20 driving license
20 operator
(对于10号的工作,员工必须是操作员。11号的雇员必须是操作员并具有驾驶执照。12号不需要任何资格)
EM:
EMNR (name)
60 John
70 Pete
80 Bert
EMKW:
EMNR KWCD
60 operator
60 driving license
70 operator
(John是持有驾驶执照的操作员; Pete是操作员,但没有执照。Bert完全没有资格)
对于John,查询应返回所有AV。对于皮特,仅12月。第十和十二;对于伯特来说,仅排在第12位。
从员工那里看,我需要知道他可以满足什么要求;因此,我需要其中所有AVKW记录都具有与当前员工匹配的AVEM记录(AV.KWCD = EM.KWCD)的AV记录。
(有一个额外的表KW,关键KWCD,列出了现有的资格,但现在已经不相关了)
我尝试了几种方法,但是似乎都没有用……找到一个匹配的资格很容易,但是emp确实需要所有要求的资格。我将如何在SQL中编写代码?
提前致谢!
我认为最直接的方法是选择具有匹配的所需资格的数量等于实际资格的数量的员工:
select * from
(
select
av.avnr,
count(*) as qualificationCount
from
avkw
inner join
av on
avkw.avnr = av.avnr
group by
av.avnr
) as qualification
inner join
(
select
em.emnr,
avkw.avnr,
count(*) qualificationCount
from
emkw
inner join
em on
emkw.emnr = em.emnr
inner join
avkw on
emkw.kwcd = avkw.kwcd
group by
em.emnr,
avkw.avnr
) as qualified
on
qualifications.avnr = qualified.avnr
where
qualifications.qualificationCont = qualified.qualificationCount
这应该列出与每个可用职位完全匹配的所有员工。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句