简体   繁体   中英

query with LEAST returning multiple rows in oracle

When I ran the below query in Oracle 11g

SELECT least(val)
FROM
(
    SELECT 1 AS val
    FROM dual
    UNION
    SELECT 2 AS val
    FROM dual
    UNION
    SELECT 3 AS val
    FROM dual
);

I was expecting a single row but it is returning multiple rows. please help me out where exactly my understanding is going wrong..

Oracle's LEAST function returns the least value in a list of expressions, eg LEAST(1, 2, 3) would return 1 . So LEAST could be used to find the minimum value across a collection of columns , eg LEAST(col1, col2, col3) . What you are seeing is to be expected, ie you are getting back three records with the smallest value of each record.

Instead, if you want the minimum over an aggregate of rows , then you should be using MIN , eg

select min(val)
from
(
    select 1 as val from dual union all
    select 2 from dual        union all
    select 3 from dual
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM