PL / SQL比较表

时间:2012-04-18 19:44:24

标签: sql oracle plsql oracle-sqldeveloper

我的任务是根据技能和可用性,使用sql和pl / sql将我的数据库中的候选人与合适的职位空缺进行匹配。

我已设法编写以下代码,将可用候选人与可用职位空缺相匹配。

 DECLARE
     CURSOR availableCandidates_cur IS
        SELECT * FROM candidate
        WHERE candidate.available = 'True';
     CURSOR availableJobs_cur IS
        SELECT *
        FROM position WHERE status = 'Open';
  BEGIN
      DBMS_OUTPUT.PUT_LINE('Available Candidates with matching vacencies');
      FOR availableCandidates_rec IN availableCandidates_cur
      LOOP
        DBMS_OUTPUT.PUT_LINE('Candidate: ' || availableCandidates_rec.firstName || ' ' ||  availableCandidates_rec.lastName);
        FOR availableJobs_rec IN availableJobs_cur
        LOOP
          IF (availableCandidates_rec.positionType = availableJobs_rec.positionType) THEN
            DBMS_OUTPUT.PUT_LINE(availableJobs_rec.positionName);
          END IF;
        END LOOP;
      END LOOP;
END;

我正在努力弄清楚如何根据匹配技能将候选人与职位匹配。有问题的表格是

candidateSkills

candidateID | skillID
1           | 2
1           | 3
2           | 1
3           | 1
3           | 3

positionSkills

positionID | skillID
1           | 1
1           | 3
2           | 1
3           | 2
3           | 3

所以例如我想输出那个

Candidate 1  Matches
position 3
Candidate 2 Matches
position 2
Candidate 3  Matches
position 2
position 3

我担心我可能会走错路,导致我的困惑。

如果有人能帮我引导我走向正确的方向,我将不胜感激。

由于

2 个答案:

答案 0 :(得分:1)

校正。候选人3匹配工作1和2,候选人2匹配工作2,候选人1匹配工作3

select distinct c.cid, j.jid 
from candidate c, jobs j
where j.sid=c.sid
and not exists
(select 'x' from jobs j2 where j2.jid=j.jid
and j2.sid not in (select c2.sid from candidate c2
where c2.cid=c.cid))

答案 1 :(得分:1)

--All candidates that match every skill in a position
select distinct candidateID, positionID
from
(
    --Match candidates and positions, count number of skills that match
    select candidateID, positionID, skills_per_position
        ,count(*) over (partition by candidateID, positionID) matched_skills
    from candidateSkills
    inner join
    (
        --Number of skills per position
        select positionID, skillID
            ,count(*) over (partition by positionID) skills_per_position
        from positionSkills
        where status = 'Open'
    ) positionSkills_with_count
        on candidateSkills.skillID = positionSkills_with_count.skillID
    where available = 'True'
)
where matched_skills = skills_per_position
order by candidateID, positionID;

使用这些脚本构建表:

create table candidateSkills as
select 1 candidateid, 2 skillID, 'True' available from dual union all
select 1 candidateid, 3 skillID, 'True' available from dual union all
select 2 candidateid, 1 skillID, 'True' available from dual union all
select 3 candidateid, 1 skillID, 'True' available from dual union all
select 3 candidateid, 3 skillID, 'True' available from dual;

create table positionSkills as
select 1 positionID, 1 skillID, 'Open' status from dual union all
select 1 positionID, 3 skillID, 'Open' status from dual union all
select 2 positionID, 1 skillID, 'Open' status from dual union all
select 3 positionID, 2 skillID, 'Open' status from dual union all
select 3 positionID, 3 skillID, 'Open' status from dual;

但是,我的结果略有不同。候选人3匹配位置1和2,而不是2和3.我希望这只是你的例子中的一个错字。

另外,我没有像你的那样格式化我的输出。使用SQL以多行格式显示结果可能有点棘手。但是,如果您想在其他进程中使用SQL,那么保留未格式化的SQL也会更有用。