1

Well, I have these columns in my table:

  1. wo_number
  2. crew_est
  3. manhour_est
  4. status ('FINISH' or NULL)
  5. progress (this isn't in the table)

I need to do this calculation for progress:

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

I need to do the calculation above and group the result by wo_number. I've looking for the answer anywhere but still no luck and I've no idea where to start.

Any help will be much appreciated. Thank you.

2
  • So, the progress will be per wo_number? Commented Dec 31, 2016 at 7:57
  • @GurwinderSingh yes, correct. Commented Dec 31, 2016 at 7:59

1 Answer 1

2

You can use case for conditional aggregation:

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;
Sign up to request clarification or add additional context in comments.

3 Comments

I forgot to add, how do I retrieve the progress result in percentage? thanks in advance, your query is working.
@MAnsyori Is the percentage not just multiplied by 100?
Correct, just multiplied the percentage by 100. Thank you so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.