CFo
CFo

Reputation: 109

Sum Column of Data where two rows are calculated before summing

I have a table with multiple columns

Code    Events    Time    AVG
A       1         1       1
B       2         2       1
C       10        5       2

I need to sum the events and total rows, but when it comes to a specific codes (B and C) I need to divide the events by two then include that number in the sum.

so the above output would be

TotalEvents    TotalTime
7              4.5

Total Events = 1 + (2/2) + (10/2) = 7

Total Time = 1+ (2/2) + (5/2) = 4.5

Any assistance is appreciated. Thanks

Upvotes: 1

Views: 32

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270523

Is this what you want?

select sum(case when code in ('B', 'C') then events / 2.0 else events end) as total_events,
       sum(case when code in ('B', 'C') then time / 2.0 else time end) as total_time
from t;

Upvotes: 1

Related Questions