I am trying to calculate the over all average speed grouped counts of vehicles traveled.

The format below shows that 5 vehicles traveled at 45km/h, 33 traveled at 55km/h, 65 at 65km/h etc.

What is the T-SQL to calculate the over all average speed of all the vehicles with the data set below? There can be up to 16 speed groups (eg 5, 15, 25, 35, 45 etc).

Count   Speed
5       45
33      55
65      65
25      75
2       85

Thanks in advance!

有帮助吗?

解决方案

I assume you are trying to get an overall average speed. If so, assuming T1 is the table name, your query should look like

select sum(count*speed)/sum(count) as overall_average from T1;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top