user1888955
user1888955

Reputation: 626

Calculate scattered rows's average in sql server?

The table looks like below:

testid stepid serverid duration
1      1      1        10
1      2      1        11
2      1      2        12
2      2      2        13
3      1      1        14
3      2      1        15
4      1      2        16
4      2      2        17

4 tests ran on two servers. Each test has 2 steps. I would like to calculate average duration of each step of all tests on the 2 servers given test id. For example, if given test ids are 1 and 2, the final table looks like below:

stepid avg_duration
1      (10 + 12) / 2
2      (11 + 13) / 2

Upvotes: 0

Views: 42

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270021

This is just a group by, right?

select stepid, avg(duration)
from t
where testid in (1, 2)
group by stepid;

Note: You might want avg(duration*1.0) if you want "normal" division.

Upvotes: 2

Related Questions