简体   繁体   English

将针对一个表的多个查询合并为一个结果集的多个字段

[英]Combine multiple queries against one table into multiple fields of one result set

ANSWER : Here's the final query built from @Michael 's answer: 答案 :这是根据@Michael的答案构建的最终查询:

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: 我有以下Emp_Skill表:

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 . 所以我们可以说我想看看这个信息对员工120,348,361370。

Query 1 for skill WLN is: 技能WLN 查询1为:

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: 技能LOC 查询2为:

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: 技能BRV 查询3为:

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: 技能KSD 查询4为:

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 , ie: 问题是: 如何有效地将这些查询组合到一个结果表中,并将它们全部合并在一起 ,即:

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? 可能来自组合的SELECT语句? (the NULL value could be anything that evaluates to FALSE in the returned result set) NULL值可以是返回结果集中评估为FALSE任何值)

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. 提前致谢。

This is done with a pivot query, which amounts to a bunch of CASE statements. 这是通过数据透视查询完成的,该查询相当于一堆CASE语句。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM