Question

ANSWER: Here's the final query built from @Michael 's answer:

SELECT 
    Emp_ID,
    MAX(CASE WHEN Skill_ID = 'WLN' THEN (Rating > 0) END) AS `WLN`,
    MAX(CASE WHEN Skill_ID = 'LOC' THEN (Rating > 0) END) AS `LOC`,
    MAX(CASE WHEN Skill_ID = 'BRV' THEN (Rating > 0) END) AS `BRV`,
    AVG(CASE WHEN Skill_ID IN ('KWH','SIC','DOL') THEN (Rating) END) > 0 AS `KSD`
FROM Emp_Skill
WHERE Emp_ID IN (120,348,361,370)
GROUP BY Emp_ID

QUESTION:

I have the following Emp_Skill table:

Skill_ID*  Emp_ID*  Rating
--------------------------
     WLN      120        6
     WLN      348        5
     WLN      361        7
     WLN      370        8
     LOC      120        7
     LOC      370        7
     LOC      348        7
     BRV      120        3
     LOC      361        6
     BRV      348        1
     KWH      348        5
     KWH      120        5
     KWH      361        5
     KWH      370        5
     SIC      361        8
     SIC      348        4
     SIC      120        2
     DOL      348        5
     DOL      361        8

and I need to know if an arbitrary number of employees have a positive skill rating for each skill.

So let's say I want to look at this info for employees 120, 348, 361 and 370.

Query 1 for skill WLN is:

SELECT `Emp_ID`, `Rating` > 0 AS `WLN` FROM `Emp_Skill` WHERE `Skill_ID` = 'WLN' AND `Emp_ID` IN (120,348,361,370)

which returns:

Emp_ID   WLN
------------
   120     1
   348     1
   361     1
   370     1

Query 2 for skill LOC is:

SELECT `Emp_ID`, `Rating` > 0 AS `LOC` FROM `Emp_Skill` WHERE `Skill_ID` = 'LOC' AND `Emp_ID` IN (120,348,361,370)

which returns:

Emp_ID   LOC
------------
   120     1
   348     1
   361     1
   370     1

Query 3 for skill BRV is:

SELECT `Emp_ID`, `Rating` > 0 AS `BRV` FROM `Emp_Skill` WHERE `Skill_ID` = 'BRV' AND `Emp_ID` IN (120,348,361,370)

which returns:

Emp_ID   BRV
------------
   120     1
   348     1

and Query 4 for skill KSD is:

SELECT `Emp_ID`, AVG(`Rating`) > 0 AS `KSD` FROM `Emp_Skill` WHERE `Skill_ID` IN ('KWH','SIC','DOL') AND `Emp_ID` IN (120,348,361,370) GROUP BY `Emp_ID`

which returns:

Emp_ID   KSD
------------
   120     1
   348     1
   361     1
   370     1

Question is: How do I efficiently combine these queries into one result table and have them all in one go, i.e.:

Emp_ID   WLN   LOC   BRV   KSD
------------------------------
   120     1     1     1     1
   348     1     1     1     1
   361     1     1  NULL?    1
   370     1     1  NULL?    1

possibly from a combined SELECT statement? (the NULL value could be anything that evaluates to FALSE in the returned result set)

OR: performance-wise, will it be better if I just query each skill rating one by one (using the 4 queries above)?

Sorry if this is elementary for some peeps out there, but I've pulled a lot of hair out and I still haven't killed this. lol. Thanks in advance.

Was it helpful?

Solution

This is done with a pivot query, which amounts to a bunch of CASE statements. I think this should get you close to what you need:

SELECT 
  Emp_ID,
  MAX(CASE WHEN Skill_ID = 'WLN' THEN  (Rating > 0) END) AS `WLN`,
  MAX(CASE WHEN Skill_ID = 'LOC' THEN  (Rating > 0) END) AS `LOC`,
  MAX(CASE WHEN Skill_ID = 'BRV' THEN  (Rating > 0) END) AS `BRV`,
  AVG(CASE WHEN Skill_ID IN ('KWH','SIC','DOL') THEN  (Rating > 0) END) AS `KSD`
FROM Emp_Skill
GROUP BY Emp_ID
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top