如何执行SUM,MINUS&在MySql中分割

时间:2015-01-05 11:03:59

标签: mysql

我有一个以下查询给我Opening_HC / Attrition / Moved / Addition:

select(select SUM(Opening_HC) from(
(select count(emp_id) as Opening_HC from hcdata where status = 

"Active" and DOJ <='2014-11-02' and sub_department='Sub_dep_07' and 

production_support='prod_sup-02')
UNION ALL
(select count(emp_id) as Opening_HC from hcdata where status = "Moved" 

and DOJ <='2014-11-02' and inactive_date >= '2014-11-02' and 

sub_department='Sub_dep_07' and production_support='prod_sup-02')
UNION ALL
(select count(emp_id) as Opening_HC from hcdata where status = 

"Attrition" and DOJ <='2014-11-02' and inactive_date >= '2014-11-02' 

and sub_department='Sub_dep_07' and production_support='prod_sup-

02'))t1) as Opening_HC, (select count(emp_id) from hcdata where 

sub_department='Sub_dep_07' and production_support='prod_sup-02' and 

status='Attrite'and inactive_date between '2014-11-02' and  '2014-11-

08') as Attrition, (select count(emp_id) from hcdata where 

sub_department='Sub_dep_07' and production_support='prod_sup-02' and 

status='Moved' and inactive_date between '2014-11-02' and  '2014-11-

08') as Moved,(select count(emp_id) from hcdata where 

sub_department='Sub_dep_07' and production_support='prod_sup-02' and 

sub_department = 'Sub_dep_07' and   DOJ between '2014-11-02' and  

'2014-11-08') as Addition

现在我要找的是Closing_HC,Entity_Attrition%&amp; Process_Attrition%

计算应该是:

Closing_HC = Opening_HC + Addition - Attrition - Moved

Entity_Attrition%=损耗/((Opening_HC + Closing_HC)/ 2)

Process_Attrition%=(损耗+移动)/((Opening_HC + Closing_HC)/ 2)

1 个答案:

答案 0 :(得分:0)

我是否正确地认为所有t1 / Opening_HC都可以重写如下:

SELECT COUNT(emp_id) Opening_HC
  FROM hcdata
 WHERE (status = 'Active')
    OR (status IN('Moved','Attrition') AND inactive_date >= '2014-11-02') 
   AND DOJ <= '2014-11-02' 
   AND sub_department = 'Sub_dep_07' 
   AND production_support = 'prod_sup-02'

并且status真的等于'Attrite'吗?