【问题标题】:Select items from table where timestamps are within an hour of eachother从表中选择时间戳在一个小时内的项目
【发布时间】:2011-07-15 15:59:13
【问题描述】:

SQL 语句是什么样的?

表格如下所示:

  • id { 整数 }
  • 姓名{ 字符不同}
  • timestamp { 时区时间戳}

    id   name       timestamp
    ---+----------+--------------------------------
    1    one        "2010-09-24 21:10:39.515+00"
    2    two        "2010-09-16 09:21:09.362+00"
    3    three      "2010-07-08 00:00:46.549+00"
    

编辑#1

这是一个更好的例子。 (在 Tometzky 的帮助下)所有结果都在 1 小时内。也就是说,对于每一行,给我 1 小时内的任何其他行:

select * from myTable order by t

 id |               t               
----+-------------------------------
  9 | 2011-07-15 18:20:20.05+02
 10 | 2011-07-15 19:05:00.05+02
 11 | 2011-07-15 19:40:20.05+02
 13 | 2011-07-15 20:31:01.05+02
 14 | 2011-07-15 20:35:11.05+02
(5 rows)

result of needed query:

 id |  matchid |  origTime                  | matchTime
----+----------+----------------------------+------------------------------
  9 |   10     | 2011-07-15 18:20:20.05+02  | 2011-07-15 19:05:00.05+02
 10 |    9     | 2011-07-15 19:05:00.05+02  | 2011-07-15 18:20:20.05+02
 10 |   11     | 2011-07-15 19:05:00.05+02  | 2011-07-15 19:40:20.05+02
 11 |   10     | 2011-07-15 19:40:20.05+02  | 2011-07-15 19:05:00.05+02
 11 |   13     | 2011-07-15 19:40:20.05+02  | 2011-07-15 20:31:01.05+02
 11 |   14     | 2011-07-15 19:40:20.05+02  | 2011-07-15 20:35:11.05+02
 13 |   11     | 2011-07-15 20:31:01.05+02  | 2011-07-15 19:40:20.05+02
 13 |   14     | 2011-07-15 20:31:01.05+02  | 2011-07-15 20:35:11.05+02
 14 |   11     | 2011-07-15 20:35:11.05+02  | 2011-07-15 19:40:20.05+02
 14 |   13     | 2011-07-15 20:35:11.05+02  | 2011-07-15 20:31:01.05+02
(10 rows)

【问题讨论】:

  • 一小时之内?如:13:00、13:59 和 14:35 是否属于同一组?
  • Postgres 的哪个版本?从 8.4 开始,Postgres 具有窗口功能,这将有助于此处。
  • @OMG :因此对于第 2 行,8:21:09 和 10:21:09 之间的任何内容都将匹配。

标签: sql postgresql


【解决方案1】:
select 
    t0.id, t1.id as matchid, 
    t0.ts as origTime, t1.ts as matchTime
from t t0
inner join t t1 on 
    t1.ts between 
        t0.ts - interval '1 hour'
        and 
        t0.ts + interval '1 hour'
    and t0.id != t1.id
;

【讨论】:

  • 但请记住,您需要在 ts 上建立索引才能获得合理的性能。
  • 谢谢,但我得到了这个查询的所有可能组合。那是一行匹配我所有的行等等。我得到了相隔多年的比赛。
【解决方案2】:

类似这样的:

$ select * from test;
 id |               t               
----+-------------------------------
  9 | 2011-07-15 18:31:01.059487+02
 10 | 2011-07-15 18:31:01.55044+02
 11 | 2011-07-15 18:31:01.850583+02
 12 | 2011-07-15 18:31:02.064435+02
 13 | 2011-07-15 18:31:02.333449+02
 14 | 2011-07-15 18:31:02.727461+02
 15 | 2011-07-15 18:31:03.279447+02
 16 | 2011-07-15 18:31:03.642454+02
 17 | 2011-07-15 18:33:34.910252+02
 18 | 2011-07-15 18:33:35.995455+02
 19 | 2011-07-15 18:33:36.45246+02
(11 rows)

$ select id, lastid
  from (
    select id,
    t-lag(t) over (order by t) as lag,
    lag(id) over (order by t) as lastid from test
  ) as _
  where lag < '1 second';
 id | lastid 
----+--------
 10 |      9
 11 |     10
 12 |     11
 13 |     12
 14 |     13
 15 |     14
 16 |     15
 19 |     18
(8 rows)

【讨论】:

  • 肯定有问题,但不确定是什么。例如,如果 9 是 10 的 1 秒,那么 10 也是 9 的 1 秒。请查看我的 EDIT #1 我根据您的示例放置了一个示例,希望它能更好地解释我需要什么。
  • 在示例之前不清楚您想要什么。 @Clodoado 给了你正确的答案。
  • 抱歉(+1:感谢您的努力和我的错误)。
猜你喜欢
  • 1970-01-01
  • 2013-05-04
  • 2021-07-23
  • 1970-01-01
  • 2014-09-13
  • 1970-01-01
  • 2016-02-16
  • 1970-01-01
  • 2021-02-05
相关资源
最近更新 更多