需要计算每天填充的储罐总量

时间:2019-06-26 06:27:11

标签: sql google-bigquery

我有来自不同坦克的数据正在被填充。最终用户似乎可以在任何时候开始加注水箱。我需要根据此数据来计算每天的储罐总数。

User ID   Tank ID   tank_capacity   tank_percent_full   server_time
XXZFR170500033  01617IJT0526Z3  1000    96  2017-05-01T02:02:23.971Z
XXZFR170500033  01617IJT0526Z3  1000    95  2017-05-01T10:58:57.691Z
XXZFR170500033  01617IJT0526Z3  1000    95  2017-05-01T14:03:36.828Z
XXZFR170500052  01617IJT0528y3  1000    95  2017-05-19T18:13:04.327Z
XXZFR170500052  01617IJT0528y3  1000    94  2017-05-19T18:27:18.657Z
XXZFR170500052  01617IJT0528y3  1000    75  2017-05-20T13:53:25.142Z
XXZFR170500052  01617IJT0528y3  1000    74  2017-05-20T17:51:54.000Z
XXZFR170500039  01617IJT0528x3  1000    66  2017-05-24T12:41:16.659Z
XXZFR170500039  01617IJT0528x3  1000    69  2017-05-24T12:54:27.501Z
XXZFR170500039  01617IJT0528x3  1000    65  2017-05-24T13:12:02.007Z

答案看起来像这样

XXZFR170500033 01617IJT0526Z3 2017-05-01 1% used 10 l
XXZFR170500052 01617IJT0526y3 2017-05-01 21% used 210 l
XXZFR170500039 01617IJT0526x3 2017-05-01 4% used 40 l

请记住,坦克01617IJT0526x3从66开始但升至69,这意味着当时该坦克已注满水,我们必须忽略这些,然后当我们看到总百分比下降时,才采取差异。因此我们在计算中只取69到65

1 个答案:

答案 0 :(得分:1)

您可以使用lag()获取填充

select t.*
from (select t.*, 
             lag(tank_percent_full) over (partition by tank_id order by server_time) as prev_tank_percent_full
      from t
     ) t
where prev_tank_percent_full < tank_percent_full;

目前尚不清楚您实际上想要什么结果。