【问题标题】:Extract records based on difference between two timestamps -postgresql根据两个时间戳之间的差异提取记录-postgresql
【发布时间】:2019-08-26 03:42:34
【问题描述】:

我有几十万个条目的以下列

date_for_trx , date_user_creation , order_number ,  email 
1554377107 .      1554377105              on234524 .    321@gmail.com
1554377000 .      1354377107              on876568 .    xyz@gmail.com
1554377105 .      1254377107              on777612 .    123@gmail.com
1554377107 .      1551177107              on123345 .    abc@gmail.com
......            .......                 .......       ........

我需要提取 date_for_trx (-) 和 date_user_creation 之间的差异 >=12 个月的电子邮件

因此,date_for_trx 为 21/03/2019 且 date_user_creation 为 21/03/2018 或更早的电子邮件

有人可以帮忙吗? 谢谢

我在这里尝试了其他问题的答案,但完全可以到达那里。

我总是得到的是两个时间戳之间的时间计算,但可以根据该计算提取电子邮件 12 个月

【问题讨论】:

  • 你说的计算在哪里?我在你的问题中没有看到这一点。
  • 我在示例数据中看不到任何时间戳。

标签: database postgresql timestamp extract


【解决方案1】:

在 PostgreSQL 中,您可以将年差乘以 12 并加上可能为负数的月份部分之间的差。

SELECT email FROM YOUR_TABLE WHERE ((DATE_PART('year', to_timestamp(date_for_trx)::date) - DATE_PART('year', to_timestamp(date_user_creation)::date)) * 12 +
          (DATE_PART('month', to_timestamp(date_for_trx)::date) - DATE_PART('month', to_timestamp(date_user_creation)::date))) >= 12;

【讨论】:

  • 我试过了,但没有得到我需要的结果,因为区别并不总是 user_created 比 date_for trx 早 6 个月或更长时间。电子邮件 date_for_trx date_user_created email1 2018-12-21 9:08 2018-12-19 email2 2018-12-07 17:58 2015-06-25 email3 2018-12-21 9:05 2016-12-17 email4 2018-12- 06 17:17 2018-06-18 电子邮件5 2018-12-21 9:47 2017-10-27 电子邮件6 2018-12-07 17:09 2015-05-07 电子邮件7 2018-12-07 16:59 2016-04- 10 email8 2018-12-07 17:30 2013-04-09 email9 2018-12-21 9:48 2018-12-20 email9 在同一月/年
  • 你可以使用这个SELECT email FROM YOUR_TABLE where to_timestamp(date_for_trx) - to_timestamp(date_user_creation) >= interval '365 days' 间隔,你可以根据需要进行更改。例如:间隔'6个月',间隔'1年',间隔'120天'
【解决方案2】:

只需将两个值相减并比较结果:

where to_timestamp(date_for_trx) - to_timestamp(date_user_creation) >= interval '12 month'

请注意,以上假设date_for_trx 大于date_user_creation。它不处理负值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-13
    相关资源
    最近更新 更多