然后,我使用此数据来生成报告,加入和分组/过滤数据.我每天都会添加数千行.
成本,收入和销售总是累积的,这意味着每个数据点来自该产品的t1,而t2是数据回溯的时间.
因此,最新的数据拉动将包括所有先前的数据,直到t1. t1,t2是Postgres中没有时区的时间戳.我目前使用的是Postgres 10.
样品:
ID,vendor_ID,product_ID,t1,t2,cost,revenue,sales1,a,2018-01-01,2018-04-18,50,200,342,b,2018-05-01,10,100,103,c,2018-01-02,12,94,d,2018-01-03,85,e,2018-25-02,76,2018-04-17,40,307,95,88,59,8,90,410,9,0-,3
成本和收入来自两个表,我将它们加入vendor_ID,product_ID和t2.
有没有办法我可以浏览所有数据并“移位”它并减去,所以我没有累积数据,而是基于时间序列的数据?
这应该在存储之前完成,还是在制作报告时更好?
作为参考,目前如果我想要一个两次变化的报告,我会做两个子查询,但它似乎倒退而不是按时间序列计算数据,只是聚合所需的间隔.
with report1 as (select ...),report2 as (select ...)select .. from report1 left outer join report2 on ...
非常感谢提前!
JR
解决方法 您可以使用LAG():Window Functions:
…returns value evaluated at the row that is offset rows before the
current row within the partition; if there is no such row,instead
return default (which must be of the same type as value). Both offset
and default are evaluated with respect to the current row. If omitted,
offset defaults to 1 and default to null.
with sample_data as ( select 1 as ID,'a'::text vendor_ID,'a'::text product_ID,'2018-01-01'::date as t1,'2018-04-18'::date as t2,50 as cost,200 as revenue,36 as sales union all select 2 as ID,'b'::text product_ID,55 as cost,34 as sales union all select 3 as ID,'2018-04-17'::date as t2,35 as cost,150 as revenue,25 as sales union all select 4 as ID,25 as cost,140 as revenue,23 as sales union all select 5 as ID,'2018-04-16'::date as t2,16 as cost,70 as revenue,12 as sales union all select 6 as ID,13 as cost,65 as revenue,11 as sales)select sd.*,coalesce(cost - lag(cost) over (partition by vendor_ID,product_ID order by t2),cost) cost_new,coalesce(revenue - lag(revenue) over (partition by vendor_ID,revenue) revenue_new,coalesce(sales - lag(sales) over (partition by vendor_ID,sales) sales_newfrom sample_data sdorder by t2 desc总结
以上是内存溢出为你收集整理的postgresql – Postgres – 计算累积数据的变化全部内容,希望文章能够帮你解决postgresql – Postgres – 计算累积数据的变化所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)