-1
我有一個表在下面的格式如何在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-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來實現這個嗎?任何幫助表示讚賞。
非常感謝一個簡單的問題做這個工程的更新通過更改SQL成'更新...設定previous_task_end_time = LAG(task_end_time)OVER(PARTITION BY ID ORDER BY task_end_time)' – Ricky
號對於更新的嘗試'UPDATE mytable a SET previous_task_end_time =(SELECT MAX(b.task_end_time)FROM mytable b WHERE b.id = a.id AND b.task_end_time
Andreas