怎么能快速检索下面表中数据

pujitan978 2020-07-03 08:35:53
表中数据如下, table, lane, pos,三列表示位置,取出所有位置( table, lane, pos相同)按时间最近的两条数据。
表的数据大概几十万行


id table lane pos wu nan create_time
1 2 1 1 aaa bbbbb 2020-6-25 01:03:01.456
2 1 2 1 cccc dddd 2020-6-25 02:00:01.456
3 1 2 1 aaa bbb 2020-6-25 03:00:01.456
4 2 1 1 ffff 333 2020-6-25 04:06:01.456
5 2 1 1 aaa bbb 2020-6-25 05:00:01.456
6 1 2 1 ffff 333 2020-6-25 06:00:01.456
7 2 1 1 aaa bbb 2020-6-25 07:00:01.456
8 2 1 1 ffff 333 2 020-6-25 08:08:01.456
9 1 1 3 ffff 333 2020-6-25 09:00:01.456
10 1 1 3 ffff 333 2020-6-25 10:00:01.456
11 1 1 3 aaa bbb 2020-6-25 11:09:01.456
12 3 2 1 ffff 333 2020-6-25 12:00:01.456
13 3 2 1 aaa bbb 2020-6-25 13:00:01.456
14 3 2 1 ffff 333 2020-6-25 14:09:01.456
15 3 2 1 ffff 333 2020-6-25 15:00:01.456



...全文
158 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
pujitan978 2020-07-12
  • 打赏
  • 举报
回复
引用 2 楼 向阳的花儿 的回复:
直接用分组函数就可以实现啦; 按照那三列分组,组内按照时间倒序
select  table,lane ,pos from (
select  table,lane ,pos,
ROW_NUMBER() over(partition by table,lane,pos order by create_time desc ) from test) as a
where n<3
没看懂哈,把sql执行报错。n 是什么?
向阳的花儿 2020-07-12
  • 打赏
  • 举报
回复
嗯,忘了赋别名了; 你可以补充一下Row_number函数的知识
select  table,lane ,pos from (
select  table,lane ,pos,
ROW_NUMBER() over(partition by table,lane,pos order by create_time desc )  as n from test) as a
where n<3
foucus、 2020-07-05
  • 打赏
  • 举报
回复
用分组函数,按照table, lane, pos分组,组内按照时间排序,用Lag函数来计算相邻行的时间差即可。
向阳的花儿 2020-07-05
  • 打赏
  • 举报
回复
直接用分组函数就可以实现啦; 按照那三列分组,组内按照时间倒序
select  table,lane ,pos from (
select  table,lane ,pos,
ROW_NUMBER() over(partition by table,lane,pos order by create_time desc ) from test) as a
where n<3

952

社区成员

发帖
与我相关
我的任务
社区描述
PostgreSQL相关内容讨论
sql数据库数据库架构 技术论坛(原bbs)
社区管理员
  • PostgreSQL社区
  • yang_z_1
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧