Search code examples
postgresqlminimum

Find minimum, after one value


Possible to find the minimum after one fixed value in postgresql?

Example:

Time_ID   Number
   1       100
   2       150
   3       200
   4       230
   5       240
   6       245
   7       250

I like to find the minimum after the ID 4. If i use the min(Number) function that case show me the 240 is the minimum, but that not true, because the 230 the minimum on the ID 4.

Have any option to include the ID 4also to the minimum search?

Thank you any help!


Solution

  • The following

    select min(number) from demo where time_id >= 4;
    

    gives you the required result

    min
    230

    I used the following schema and test data

    
    create table demo (
        Time_ID   integer,
        Number       integer
    );
        
    insert into demo (time_id, number) values 
        (   1  ,     100),
        (   2    ,   150),
        (   3     ,  200),
        (   4      , 230),
        (   5      , 240),
        (   6      , 245),
        (   7      , 250);
    

    View on DB Fiddle