【问题标题】:postgres: computing the standard deviation value for each trippostgres:计算每次行程的标准差值
【发布时间】:2020-06-30 15:20:50
【问题描述】:

当前表格如下:

SELECT * FROM mytable;
 user_id |     trip_id      |    lat    |    lon     | sampling_rate 
---------+------------------+-----------+------------+---------------
       2 | 1020081023055305 | 39.984094 | 116.319236 |              
       2 | 1020081023055305 | 39.984198 | 116.319322 |             1
       2 | 1020081023055305 | 39.984224 | 116.319402 |             5
       2 | 1020081023055305 | 39.984211 | 116.319389 |             5
       2 | 1020081023055305 | 39.984217 | 116.319422 |             5
       2 | 2020081023124523 | 39.927938 | 116.338967 |              
       2 | 2020081023124523 | 39.927527 | 116.338899 |             1
       2 | 2020081023124523 | 39.926516 | 116.338048 |            30
       2 | 2020081023124523 | 39.926496 | 116.338094 |             5
       2 | 2020081023124523 | 39.926498 |  116.33814 |             5
       5 | 4020081023175852 | 39.999974 | 116.327149 |              
       5 | 4020081023175852 | 40.000011 | 116.327161 |             6
       5 | 4020081023175852 | 40.000008 | 116.327135 |             5
       5 | 4020081023175852 | 40.000016 | 116.327126 |             5
       5 | 4020081023175852 | 40.000003 | 116.327098 |             5
       5 | 4020081023175852 | 40.000019 | 116.327071 |             5
       5 | 4020081023175852 | 40.000132 | 116.327086 |             5

然后我想计算每次行程sampling_rate 的平均值和标准差(注意每次行程开始时sampling_rate 的值是0。

SELECT  AVG(sample_stat)AS mean_rate_val, STDDEV_POP(sample_stat) AS sampling_std
FROM (
SELECT COUNT(1) sample_stat FROM mytable GROUP BY trip_id
) sample_analysis

查询返回整个 sampling_rate 列的均值和 std_dev。

【问题讨论】:

    标签: sql postgresql aggregate-functions


    【解决方案1】:

    然后我想计算每次行程的采样率的均值和标准差

    这是你想要的吗?

    select user_id, trip_id, avg(sampling_rate), stddev_pop(sampling_rate)
    from mytable
    group by user_id, trip_id;
    

    【讨论】:

    • 啊,亲爱的我怎么没想到这个……我总是觉得很难。谢谢。
    猜你喜欢
    • 2019-09-17
    • 2015-06-17
    • 1970-01-01
    • 1970-01-01
    • 2019-08-15
    • 2023-04-11
    • 2018-06-17
    • 1970-01-01
    相关资源
    最近更新 更多