Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: SQL Server
Expertise: Beginner
Mar 24, 1997

Matching Employees' Skills with Jobs

Question:
[Joe Celko's Employment Agency Puzzle]

You are running an employment agency that has requests for jobs and applications from candidates. Both of these forms include a list of the skills required or offered, and whether this is a primary or secondary skill. The tables look like this:

    CREATE TABLE CandidateSkills
        (candidateid INTEGER NOT NULL PRIMARY KEY, 
        name CHAR(20) NOT NULL, 
        skillid INTEGER NOT NULL, 
        skilllevel INTEGER NOT NULL CHECK (skilllevel IN (1, 2)), 
        PRIMARY KEY (candidateid, skillid, skilllevel)
        );

    CREATE TABLE JobSkills
        (jobid INTEGER NOT NULL, 
        skillid INTEGER NOT NULL, 
        skilllevel INTEGER NOT NULL CHECK (skilllevel IN (1, 2)), 
        PRIMARY KEY (jobid, skillid, skilllevel)
        );
We need to write some queries that will match up candidates with jobs. But our service offers three kinds of matchings for a job:

1. A list of candidate names who each exhibit ALL the primary skills and ANY of the secondary skills.

2. A list of candidate names who each exhibit ALL the primary skills and ALL of the secondary skills.

3. A list of candidate names who each exhibit ALL the primary skills and (n) number of the secondary skills.

Answer:
To find people with all the primary skills:

    SELECT C1.name, C1.candidateid, J1.jobid
        FROM JobSkills AS J1
    LEFT OUTER JOIN
        Candidates AS C1
        ON J1.skilllevel = C1.skilllevel
            AND J1.skillid = C1.skillid;
    GROUP BY C1.name, C1.candidateid, J1.jobid
    HAVING ((SELECT COUNT(skillid) 
        FROM Candidates AS C2
        WHERE C1.candidateid = C2.candidateid
            AND C2.skilllevel = 1)
            >= (SELECT COUNT(skillid) 
                FROM JobSkills AS J2
                WHERE J1.jobid = J2.jobid
                    AND J2.skilllevel = 1))
                    AND [add one of the following];
a) All the secondary skills:
                    ((SELECT COUNT(skillid) 
                        FROM Candidates AS C2
                        WHERE C1.candidateid = C2.candidateid
                            AND C2.skilllevel = 2)
                            >= (SELECT COUNT(skillid) 
                                FROM JobSkills AS J2
                                WHERE J1.jobid = J2.jobid
                                    AND J2.skilllevel = 2))
b) Any of the secondary skills:
                    ((SELECT COUNT(skillid) 
                        FROM Candidates AS C2
                        WHERE C1.candidateid = C2.candidateid
                            AND C2.skilllevel = 2) > 0)
c) exactly (n) of the secondary skills:
                    ((SELECT COUNT(skillid) 
                        FROM Candidates AS C2
                        WHERE C1.candidateid = C2.candidateid
                            AND C2.skilllevel = 2) = :n)

Puzzle provided courtesy of:
Joe Celko
71062.1056@compuserve.com

DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date