【问题标题】:add date(created_at) to pervious data on postgresql将 date(created_at) 添加到 postgresql 上的先前数据
【发布时间】:2018-01-22 09:16:11
【问题描述】:

我有一个 SQL 数据库,它在一周中的 5 天(周一到周五)记录一些数据,我有一行设置一天中的时间,数据是这样创建的 14:25:16 但我没有添加一列记录日期。(实际上,我从某个有时间但没有日期的地方下载数据) PostgreSQL 上是否有任何隐藏数据可以保存 created_at 之类的内容,或者是否有任何强大的算法可以将日期添加到我的数据库中?我想为我的数据创建一个时间序列,如果没有日期,我的数据就没用了。

我尝试用 python 和 pandas 数据框做一些事情来构建一种机制来发现小时数何时增加: hour 23 23--> this is the row that day will change after 00 00

但我发现我的数据有一些警告,因为有几天没有下载新闻数据。

【问题讨论】:

  • 从时间生成人工日期很容易,但我不明白你怎么能用间隙做到这一点 - 你怎么知道间隙的长度?.. Postgres 也不跟踪时间戳数据调整左右(如果你的意思是这样的话)
  • 我的问题也是?我下载了一些货币和黄金价格数据,其中一些更新频率更高(每分钟),而另一些更新频率更低(每周两次),因此我很难操纵差距。是的,跟踪时间戳正是我想要的。
  • 您对制作人工日期有何想法?

标签: python sql postgresql pandas date


【解决方案1】:

使用一些逻辑和窗口函数来模拟人工时间戳很容易,例如:

with c(i,t) as (values(1,'11:0:00'::time),(2,'23:00'),(3,'02:00'),(4,'01:00'),(5,'23:00'),(6,'01:00'),(7,'02:00'))
, d(s) as (values('1980.09.19'::timestamp))
, n as (select *,case when lag(t) over(order by i) > t then i else 0 end c from c)
, f as (select *,s+ concat( (dense_rank() over (order by c nulls first)-1)::text ,' days')::interval art from n join d on true)
select i,t, case when art = s then coalesce(lag(art) over (order by i),s) else art end cs 
from f order by i;
 i |    t     |         cs
---+----------+---------------------
 1 | 11:00:00 | 1980-09-19 00:00:00
 2 | 23:00:00 | 1980-09-19 00:00:00
 3 | 02:00:00 | 1980-09-20 00:00:00
 4 | 01:00:00 | 1980-09-21 00:00:00
 5 | 23:00:00 | 1980-09-21 00:00:00
 6 | 01:00:00 | 1980-09-22 00:00:00
 7 | 02:00:00 | 1980-09-22 00:00:00
(7 rows)

但您无法猜测间隙的大小。所以如果你没有关于它的信息 - 你不能构建它......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 2010-09-21
    • 2013-09-01
    • 2020-06-21
    • 2018-12-29
    相关资源
    最近更新 更多