2016-12-31 186 views
1

嗯,我有我的表中这些列:MySQL查询多重条件和SUM函数

  1. wo_number
  2. crew_est
  3. manhour_est
  4. 状态( '完成' 或NULL)
  5. 进展(这不是在表中)

我需要做这个计算p rogress:

progress = SUM(crew_est * manhour_est) WHERE status = 'FINISH')/(SUM(crew_est * manhour_est) WHERE status IS NULL); 

我需要做上面的计算并将结果按wo_number分组。我一直在寻找答案,但仍然没有运气,我不知道从哪里开始。

任何帮助将非常感激。 谢谢。

+0

因此,进度将每wo_number? – GurV

+0

@GurwinderSingh是的,正确的。 –

回答

2

可以使用的情况下有条件聚集:

select wo_Number, 
    100.0 * SUM(case 
      when status = 'FINISH' 
       then crew_est * manhour_est 
      else 0 
      end)/SUM(case 
      when status is null 
       then crew_est * manhour_est 
      else 0 
      end) progress_percentage 
from your_table 
group by wo_number; 
+0

我忘了补充,如何以百分比检索进度结果?在此先感谢您的查询正在工作。 –

+0

@MAnsyori百分比是不是乘以100? – GurV

+0

正确,只是将百分比乘以100.非常感谢。 –