Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Postgresql中查找下一次访问?_Postgresql - Fatal编程技术网

如何在Postgresql中查找下一次访问?

如何在Postgresql中查找下一次访问?,postgresql,Postgresql,我有一个名为visit\u record的表: customer_id visit_month 000001 1 000001 3 000001 7 000001 8 000002 2 000002 3 ... 我想输出以下内容: customer_id visit_month next_visit 000001 1 3 000001 3 7 000001

我有一个名为
visit\u record
的表:

customer_id visit_month
000001      1
000001      3
000001      7
000001      8
000002      2
000002      3
      ...
我想输出以下内容:

customer_id visit_month next_visit
    000001      1       3
    000001      3       7
    000001      7       8
    000001      8       null
    000002      2       3
    000002      3       5
               ...
有什么建议吗?

使用
lead()

select customer_id, 
       visit_month,
       lead(visit_month) over (partition by customer_id order by visit_month) as next_visit
from visit_record
order by customer_id, visit_month;