How should I calculate the average speed by road segment for multiple segments?

前端 未结 2 529
暖寄归人
暖寄归人 2021-01-29 16:30

I have a table of driver speeds and road segments:

driver_lpr    |   segment    |    speed
  0000001     |       A      |     30
  0000002     |       B      |           


        
2条回答
  •  鱼传尺愫
    2021-01-29 17:03

    When averaging speeds, the harmonic mean is in need.

    The straight forward AVG() approach is wrong, the arithmetic mean yields the wrong result for average velocity.

    There is no predefined function for the harmonic mean, but it could be achieved with this query:

    SELECT segment,
           COUNT(*)/SUM(1e0/speed) AS avg_speed
    FROM T 
    GROUP BY segment
    

    SQL Fiddle

提交回复
热议问题