在sql中创建伪链接列表 [英] creating a pseudo linked list in sql

查看:66
本文介绍了在sql中创建伪链接列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下列的表

I have a table that has the following columns

table: route
columns: id, location, order_id

,它的值例如

id, location, order_id
1, London, 12
2, Amsterdam, 102
3, Berlin, 90
5, Paris, 19

是否有可能在postgres中执行sql select语句,该语句将返回每一行以及id下一个最高的order_id?所以我想要类似...

Is it possible to do a sql select statement in postgres that will return each row along with the id with the next highest order_id? So I want something like...

id, location, order_id, next_id
1, London, 12, 5
2, Amsterdam, 102, NULL
3, Berlin, 90, 2
5, Paris, 19, 3

谢谢

推荐答案

select 
  id, 
  location, 
  order_id,
  lag(id) over (order by order_id desc) as next_id
from your_table

这篇关于在sql中创建伪链接列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆