在Postgresql中添加基于时间的分类变量
我有一个数据库在Postgresql中添加基于时间的分类变量,sql,database,postgresql,Sql,Database,Postgresql,我有一个数据库 CREATE TABLE data.atc full ( index character varying, serial_nbr bigint, date_ date, time_ time without time zone, ) 数据看起来像 "35107592";40873618845;"2016-01-20";"10:33:00" "35107593";1600473508;"2016-01-20";"
CREATE TABLE data.atc full
(
index character varying,
serial_nbr bigint,
date_ date,
time_ time without time zone,
)
数据看起来像
"35107592";40873618845;"2016-01-20";"10:33:00"
"35107593";1600473508;"2016-01-20";"10:33:00"
"35107594";40872283641;"2016-01-20";"10:34:00"
"35107596";713186627;"2016-01-20";"10:34:00"
"35107598";42568856045;"2016-01-20";"10:34:00"
"35107600";1836730779;"2016-01-20";"10:34:00"
"35107601";713323741;"2016-01-20";"10:34:00"
"35107603";713985185;"2016-01-20";"10:34:00"
"35107604";40872125248;"2016-01-20";"10:34:00"
"35107605";711995425;"2016-01-20";"10:34:00"
"35107606";40877157402;"2016-01-20";"10:34:00"
"35107607";711492382;"2016-01-20";"10:34:00"
我想向数据库中添加另一列,该列的值为0、1、2。。。对于特定日期和特定序列号的不同时间条目。因此,对于每个序列号,我可以查看当天的第一个条目和类似的第二个条目,依此类推
我从添加列开始
ALTER TABLE data.atc ADD COLUMN od
我想不出任何办法来做这件事。请帮助。为什么不直接使用查询
select a.*,
row_number() over (partition by serial_nbr, date_ order by time_) as seqnum
from data.atc a;
您可以将此值存储在数据中,但要使其保持最新状态是一件痛苦的事情。更新发生,新数据进入。如何分配新值
您可以使用更新存储该值:
update data.atc a
set od = seqnum
from (select a.*,
row_number() over (partition by serial_nbr, date_ order by time_) as seqnum
from data.atc a
) aa
where aa.serial_nbr = a.serial_nbr and aa.date_ = a.date_ and
aa.time_ = a.time_;
谢谢你,戈登。我不需要更新数据。但是如何将seqnum保存到我的数据库中呢?您希望实现什么目标?这里没有一个明确的问题。