user4359659
user4359659

Reputation: 159

Oracle database - select a minimum of a select result

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

Answers (2)

Abhi_007
Abhi_007

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

Aramillo
Aramillo

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

Related Questions