I have a table that records when tasks were completed. Tasks belong to a workflow, but in this example I’m just trying to get the LAG working.
I would like to find information about how long each task takes.
I’ve tried:
select
completed_date,
lag(completed_date) over (order by id) prevrow,
prevrow - completed_date
from
task_complete
where workflow_id = 1
But this results in an error. Is there a way to calculate the difference between the current row and previous row?
According to the Oracle documentation:
This means that you can’t use the results of an analytic function in the current level of the query.
There are two solutions to this. You could either include the
LAGfunction as often as necessary in the select list. Notice that this is what you would do even with a normal function because you can’t refer to the column alias (prevrow) elsewhere in the same select list anyway.OR you can use a subquery to get the results: