sql查询Q1&Q2?

时间:2019-02-24 05:12:18

标签: mysql

Q1:确定所有符合职位资格的候选人及其职位
问题2:确定所有符合特定职位条件的候选人及其职位(即没有多余的技能)

create table JobSkills (
   job char(20),
   skill char(20),
   primary key(job, skill)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

create table Candidates (
   candidate char(20),
   skill char(20)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

insert into JobSkills values
('DB Architect','SQL'),
('DB Architect','DB Design'),
('DB Architect','Python'),
('DB Architect','Team Player'),
('DB Architect','Passionate'),
('Front End Developer','JAVA'),
('Front End Developer','C#'),
('Front End Developer','Team Player'),
('Front End Developer','Passionate'),
('Office Manager','Passionate'),
('Office Manager','Office');

insert into Candidates values
('Ami', 'SQL'),
('Ami', 'DB Design'),
('Ami', 'Team Player'),
('Ami', 'Passionate'),
('Xi', 'SQL'),
('Xi', 'Python'),
('Xi', 'DB Design'),
('Xi', 'Team Player'),
('Xi', 'Passionate'),
('DJ', 'JAVA'),
('DJ', 'C#'),
('DJ', 'Team Player'),
('DJ', 'Passionate'),
('DJ', 'Python'),
('Steve', 'Passionate'),
('Steve', 'Leader'),
('Darrin', 'SQL'),
('Darrin', 'DB Design'),
('Darrin', 'C#'),
('Darrin', 'Python'),
('Darrin', 'JAVA'),
('Darrin', 'Office'),
('Darrin', 'Team Player'),
('Darrin', 'Leader'),
('Darrin', 'Passionate');

1 个答案:

答案 0 :(得分:0)

我认为候选人至少具有所期望的职位/职位的所有技能,才是“合格”。

那么对于第一个问题,我了解到候选人的技能可能比预期的要多。

为此,带有GROUP子句的HAVING BY应该可以解决问题。
工作的总技能应等于候选人的匹配技能。

-- Q1: Identify all candidates who are qualified for a position
--     and their positions
--
SELECT c.candidate, j.job AS position
FROM JobSkills AS j
JOIN Candidates AS c ON c.skill = j.skill
JOIN 
(
   SELECT job, COUNT(DISTINCT skill) AS TotalSkills 
   FROM JobSkills 
   GROUP BY job
) jobtot ON jobtot.job = j.job
GROUP BY c.candidate, j.job
HAVING COUNT(DISTINCT c.skill) = MAX(jobtot.TotalSkills)
ORDER BY c.candidate, j.job, COUNT(DISTINCT c.skill) DESC;

结果:

candidate   job
---------   --------------------
Darrin      DB Architect
Darrin      Front End Developer
Darrin      Office Manager
DJ          Front End Developer
Xi          DB Architect

但是第二,我理解这一点,因为该候选人不能拥有工作不需要的任何其他技能。

然后有点复杂。
因为那样的话,候选人的全部技能也必须加入进来。

-- Q2: Identify all candidates who are qualified for a particular position
--     and the their position (i.e., no redundant skills)
--
SELECT cj.candidate, cj.job AS position
FROM 
(
  SELECT c.candidate, j.job, 
   COUNT(DISTINCT c.skill) AS TotalMAtchingSkills 
  FROM Candidates c
  JOIN JobSkills j ON j.skill = c.skill
  GROUP BY c.candidate, j.job
) cj
JOIN
(
   SELECT candidate, 
    COUNT(DISTINCT skill) AS TotalSkills 
   FROM Candidates 
   GROUP BY candidate
) candtot ON candtot.candidate = cj.candidate 
         AND candtot.TotalSkills = cj.TotalMAtchingSkills
JOIN
(
   SELECT job, 
    COUNT(DISTINCT skill) AS TotalSkills 
   FROM JobSkills 
   GROUP BY job
) jobtot ON jobtot.job = cj.job
        AND jobtot.TotalSkills = cj.TotalMAtchingSkills
ORDER BY cj.candidate, cj.job;

结果:

candidate   job
----------  ------------
Xi          DB Architect