jsmabbas jsmabbas - 7 months ago 9
SQL Question

SQL Query to Show next date

How do I write a query which will return the next date.

Here is example, I want query to populate the Next_Date column

Thanks

Employee_ID Date Point Next_Date
53 07/31/2015 1 12/02/2015
53 12/02/2015 1 01/12/2016
53 01/12/2016 1 02/10/2016
53 02/10/2016 1


I used the following query but getting the Null

SELECT
TOP 1 att.attend_date
FROM
Attendance att
WHERE
att.ID_Employee=att.ID_Employee and
att.attend_date > att.attend_date
ORDER BY
att.attend_date ASC

mo2 mo2
Answer

Lead should work, depending on which DB you are using.

select 
  employee_id, 
  date, 
  point,
  lead(date) over (partition by employee_id order by date) as next_date
from your_table