简体   繁体   English

Postgresql:在插入带有时间条件的新记录时更新旧记录

[英]Postgresql: update old record when the new one is inserted with time condition

So, I have a postgresql table which keep appending new records with different item 所以,我有一个postgresql表,它继续追加不同项目的新记录

item      period                     cost    cost_diff
---------------------------------------------------------
 bag    2019-03-15T18:15:00.000Z     100         0
 shoe   2019-03-15T18:15:00.000Z     200         0

So, when records come in, their cost_diff will be 0. But when the new one come like this 所以,当记录进来时,他们的cost_diff将为0.但是当新记录出现时

item      period                     cost    cost_diff
---------------------------------------------------------
 bag    2019-03-15T18:15:00.000Z     100         0
 shoe   2019-03-15T18:15:00.000Z     200         0
 bag    2019-03-15T18:30:00.000Z     150         0
 shoe   2019-03-15T18:45:00.000Z     300         0

The cost_diff of the old record will be updated by using (new cost - old cost) but it will be updated if and only if the period is the next 15 minutes in which the data will be insert at time of 0, 15,30 and 45 minute. 旧记录的cost_diff将通过使用(新成本 - 旧成本)进行更新,但是当且仅当时间段是在0,15,30时插入数据的下一个15分钟时才会更新。 45分钟。

item      period                     cost    cost_diff
---------------------------------------------------------
 bag    2019-03-15T18:15:00.000Z     100        50 (150-100)
 shoe   2019-03-15T18:15:00.000Z     200         0 (no update)
 bag    2019-03-15T18:30:00.000Z     150         0
 shoe   2019-03-15T18:45:00.000Z     300         0

Table above shows that the newer record for bag that has 15 minutes range (18:15->18:30) are inserted so the bag row with period of 18:15 will update the cost_diff column to 50 from the cost from 18:30 minus with cost from 18:15 which will be 150 - 50 = 100. While the old shoe row will not be updated (still 0) because the newer shoe record that come in is not the next 15 minute (18:15->18:45) and it will be update when the shoe row with period of 18:30 insert in the table and so on for other records as well(there are many item, not just show and bag as shown). 上面的表格显示了包含15分钟范围(18:15-> 18:30)的行李的较新记录,因此行周期为18:15的行将从18:30开始将cost_diff列更新为50减去18:15的费用,这将是150 - 50 = 100.虽然旧的鞋排不会更新(仍然是0),因为进入的新鞋记录不是接下来的15分钟(18:15-> 18) :45)当表格中插入18:30的鞋排等时会更新其他记录(有很多项目,不仅仅是show和bag如图所示)。

So, how can I create a query base on this problem, because of the record will keep coming into this table, can this be done purely using sql query or do I need to use python to help with this (I am doing an etl pipeline in which this task include in the transform process) 那么,我怎样才能根据这个问题创建一个查询,因为记录将继续进入这个表,这可以纯粹使用sql查询完成,还是需要使用python来帮助解决这个问题(我正在做一个etl管道其中此任务包含在转换过程中)

Thank you 谢谢

You can do this with a query. 您可以使用查询执行此操作。 Use lead() : 使用lead()

select t.*,
       (case when lead(period) over (partition by item order by period) < period + interval '15 minute'
             then lead(cost) over (partition by item order by period) - cost
             else 0
       ) as cost_diff
from t;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当插入新记录时,从另一个表插入一个表 - insert into a table from another table when a new record is inserted 当旧= [1,2]新= [2,3]时,SQLAlchemy如何更新子代 - SQLAlchemy how to update children when old = [1, 2] new = [2, 3] Django 模型从创建新记录而不是更新旧记录 - Django model from create a new record instead of updating old one 将记录插入 PostgreSQL 表并过期旧记录 - Insert record into PostgreSQL table and expire old record 尝试更新记录时django admin崩溃,但插入新记录时不崩溃 - django admin crashes when trying to update record but not when inserting a new one 如何在django中更新现有记录而不是创建新记录? - How to update an existing record instead of creating a new one in django? 旧线程完成后如何启动新线程? - How to start a new thread when old one finishes? 使用 Python 获取为 Postgresql SERIAL KEY 插入的最后一条记录的 ID - Getting the id of the last record inserted for Postgresql SERIAL KEY with Python 将熊猫数据框中的某些行复制到新行(时间条件) - Copy certain rows from pandas dataframe to a new one (Time condition) 如何用另一个数据帧更新一个熊猫数据帧(更新旧数据并添加新数据) - How to update one pandas dataframe with another dataframe (update the old data and add new data)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM