Reputation: 159
I'd like to get the minimum value of a column returned by a select statement:
select lead(place) over (order by place) - place as gap
from viewers
I wanted to do it using the where clause, but apparently I cannot refer to my 'gap' column in the condition.
Upvotes: 0
Views: 97
Reputation: 37
Just treat the result given by your query as a table from which you want to select minimum .
select min(gap) as MinimumGap
from (
select lead(place) over (order by place) - place as gap
from viewers
) ;
Upvotes: 2
Reputation: 3216
You have to do it as a subquery:
select min(gap) from (
select lead(place) over (order by place) - place as gap
from viewers)
Upvotes: 6