我应该如何在同一个表上加入两个查询结果?

时间:2018-04-02 09:40:18

标签: mysql

我有以下两个查询,我有两个加入。

SELECT count(staff_id) FROM employee_kras as e WHERE appraisal_type_id is null
union all
SELECT count(staff_id) from employee_kras as e
where Team_KRA_Status='approved' AND Appraisal_status IS NULL
union all
SELECT count(staff_id) from employee_kras as e where appraisal_status='not submitted' and
    Appraisal_Type_ID IS NOT NULL
union all
SELECT count(staff_id) from employee_kras as e where
    Appraisal_status='submitted' AND Mgr_Rating_id is null

以下查询:

SELECT staff_id, 
CASE WHEN appraisal_status='not submitted' and Appraisal_Type_ID IS NOT NULL 
THEN 'Inprogress' 
WHEN  Appraisal_status='submitted' AND Mgr_Rating_id is null 
THEN 'submitted' 
WHEN Team_KRA_Status='approved' AND Appraisal_status IS NULL 
THEN 'approved' 
WHEN appraisal_type_id is null then 'not started' 
ELSE 'submitted' END AS appraisal_status
from employee_kras ORDER BY staff_id;

1 个答案:

答案 0 :(得分:1)

您无法加入这些查询。有不同程度的聚合。您可以使用以下查询来获得您想要的resulat,请注意,我使用静态方法来完成这项工作:

SELECT 'not started' Status , count(staff_id) FROM employee_kras as e WHERE appraisal_type_id is null
union all
SELECT 'approved' Status   , count(staff_id) from employee_kras as e
where Team_KRA_Status='approved' AND Appraisal_status IS NULL
union all
SELECT 'Inprogress' Status , count(staff_id) from employee_kras as e where appraisal_status='not submitted' and
    Appraisal_Type_ID IS NOT NULL
union all
SELECT 'submitted' Status  , count(staff_id) from employee_kras as e where
    Appraisal_status='submitted' AND Mgr_Rating_id is null