Java 如何在postgresql中领先?

Java 如何在postgresql中领先?,java,sql,database,postgresql,Java,Sql,Database,Postgresql,我有以下格式的表格 id task_start_time task_end_time __ _______________ _____________ 1 2017-03-21 00:09:10 2017-03-21 00:12:18 1 2017-03-21 00:12:19 2017-03-21 00:12:56 1 2017-03-21 00:12:57 2017

我有以下格式的表格

id       task_start_time            task_end_time
__       _______________            _____________
1       2017-03-21 00:09:10     2017-03-21 00:12:18
1       2017-03-21 00:12:19     2017-03-21 00:12:56
1       2017-03-21 00:12:57     2017-03-21 00:13:10
2       2017-03-21 10:09:10     2017-03-21 10:25:34
2       2017-03-21 10:25:34     2017-03-21 11:09:10
2       2017-03-21 11:09:10     2017-03-21 11:21:39
3       2017-03-21 12:09:10     2017-03-21 12:19:19
3       2017-03-21 12:19:19     2017-03-21 12:29:19
3       2017-03-21 12:29:10     2017-03-21 12:39:10
在此表中,我需要通过添加另一列来更新该表,该列是特定id的task_end_time的前一行,该列是id的task_end_time

id       task_start_time            task_end_time        previous_task_end_time

__       _______________            _____________       ______________________
1       2017-03-21 00:09:10     2017-03-21 00:12:18             NA
1       2017-03-21 00:12:19     2017-03-21 00:12:56             2017-03-21 00:12:18 
1       2017-03-21 00:12:57     2017-03-21 00:13:10             2017-03-21 00:12:56
2       2017-03-21 10:09:10     2017-03-21 10:25:34             2017-03-21 10:25:34
2       2017-03-21 10:25:34     2017-03-21 11:09:10             2017-03-21 11:09:10
2       2017-03-21 11:09:10     2017-03-21 11:21:39             2017-03-21 11:21:39
3       2017-03-21 12:09:10     2017-03-21 12:19:19             2017-03-21 12:19:19
3       2017-03-21 12:19:19     2017-03-21 12:29:19             2017-03-21 12:29:19
3       2017-03-21 12:29:10     2017-03-21 12:39:10             2017-03-21 12:39:10
那么,在postgresql中可以很容易地做到这一点,还是需要使用JAVA来实现这一点?感谢您的帮助。

使用lag

选择id、任务开始时间、任务结束时间 ,LAGtask_end_时间在分区上按id排序,按任务结束时间作为上一个任务结束时间 从…起 有关运行示例,请参见

id任务\u开始\u时间任务\u结束\u时间上一个任务\u结束\u时间 1 2017-03-21 00:09:10 2017-03-21 00:12:18空 1 2017-03-21 00:12:19 2017-03-21 00:12:56 2017-03-21 00:12:18 1 2017-03-21 00:12:57 2017-03-21 00:13:10 2017-03-21 00:12:56 2 2017-03-21 10:09:10 2017-03-21 10:25:34空 2 2017-03-21 10:25:34 2017-03-21 11:09:10 2017-03-21 10:25:34 2 2017-03-21 11:09:10 2017-03-21 11:21:39 2017-03-21 11:09:10 3 2017-03-21 12:09:10 2017-03-21 12:19:19空 3 2017-03-21 12:19:19 2017-03-21 12:29:19 2017-03-21 12:19:19 3 2017-03-21 12:29:10 2017-03-21 12:39:10 2017-03-21 12:29:19
非常感谢一个快速的问题,通过将sql更改为update,这对更新有效吗。。。设置上一个任务\u结束\u时间=LAGtask\u结束\u按任务\u结束\u时间编号按id划分的时间。对于更新,请尝试更新mytable a设置上一个任务结束时间=从mytable b中选择MAXb.task结束时间,其中b.id=a.id和b.task结束时间