Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.misc -> Re: sql plus max function query
A copy of this was sent to sdgreenwood_at_ybs.co.uk
(if that email address didn't require changing)
On Mon, 01 Feb 1999 09:35:04 GMT, you wrote:
>Max function query
>-----------------------------
>
>select max(start_date)
>from accounts
>where ........etc....
>
>If there are no rows to satisfy the above query sql plus does not say 'No
>Rows Selected'.
>
>If I ran the above query without the max function 'No Rows Selected' is
>displayed.
>
>The problem is that if the above query is set up in a cursor then %notfound
>would not work, therefore processing would continue without testing whether
>any rows have been found.
>
>I know that putting NVL(max(start_date,sysdate)) would work, but can anybody
>tell me why the max function works differently.
>
aggregates return rows by GROUP BY variables. An aggregate with no group by always returns 1 row. Thats just the way they work.
Try something like this:
SQL> select max(created) from all_users where 1=0 group by 1;
no rows selected
SQL> select max(created) from all_users where 1=1 group by 1;
MAX(CREAT
Alternatively, an inline view may be used to achieve the same effect:
SQL> select * from ( select max(created) created from all_users where 1=0)
2 where created is not null
3 /
no rows selected
SQL> select * from ( select max(created) created from all_users where 1=1)
2 where created is not null
3 /
CREATED
Thomas Kyte
tkyte_at_us.oracle.com
Oracle Service Industries
Reston, VA USA
--
http://govt.us.oracle.com/ -- downloadable utilities
Anti-Anti Spam Msg: if you want an answer emailed to you, you have to make it easy to get email to you. Any bounced email will be treated the same way i treat SPAM-- I delete it. Received on Mon Feb 01 1999 - 07:19:11 CST
![]() |
![]() |