“Partition By”给出了不正确的值

9gm1akwq  于 2022-09-21  发布在  PostgreSQL
关注(0)|答案(1)|浏览(201)
battery_pct   tstamp    charging    phone_id
90            t1        yes         12
91            t2        yes         22
95            t3        no          22 
89            t4        no          22
87            t5        no          22
80            t6        no          22
78            t7        yes         22
85            t8        yes         4
50            t9        no          4
40            t10       no          4
38            t11       no          4
20            t12       yes         4

我想计算电池耗损率为:电池电量变化/所用时间

当收费为“否”(夹在2个“是”之间)时,应该计算所有Windows的电费,然后取这些电费的平均值。

因此,对于此数据集,它应该是:

95-80/T6-T3=phone_id 22的速率

50-38/t11-t9=phone_id 4的速率

平均费率=(费率1+费率2)/2

请注意,对于数据中的每个phone_id,可以有多个no窗口。我必须找到所有电话ID的平均速率。即一个包含所有电话的平均速率的值。

以下是我当前的代码,它没有给出任何错误,但返回了一个不可信的值-

with discharge_intervals as (
  select battery_pct, tstamp, 
         sum((charging = 'yes')::int) over (partition by phone_id order by tstamp) as ival_number,
         charging = 'no' as keep
    from dataset
), interval_rates as (
  select ival_number, 
         (max(battery_pct) - min(battery_pct)) 
           / extract(epoch from max(tstamp) - min(tstamp)) as ival_rate
    from discharge_intervals
   where keep
   group by ival_number
)
select avg(ival_rate) 
  from interval_rates;
gopyfrb3

gopyfrb31#

您的interval_rates是在没有按电话分组的情况下计算的,但应该是按电话分组的。ival_numberphone_id分区,但这只是意味着多部手机将创建具有相同ival_number的行。你会想要使用

with discharge_intervals as (
  select battery_pct, tstamp, phone_id,
--                            ^^^^^^^^^
         sum((charging = 'yes')::int) over (partition by phone_id order by tstamp) as ival_number,
         charging = 'no' as keep
    from dataset
), interval_rates as (
  select (max(battery_pct) - min(battery_pct)) 
           / extract(epoch from max(tstamp) - min(tstamp)) as ival_rate
    from discharge_intervals
   where keep
   group by phone_id, ival_number
--          ^^^^^^^^^
)
select avg(ival_rate) 
  from interval_rates;

相关问题