I need to get the max value on a field of a table but I can't use max or any other aggregation function nor cursors. For example I need to get the max value of the field amount on the table Sales.
1 Answer
A couple of ways:
1. Sort the column descending and get the 1st row:
select top 1 amount from sales order by amount DESC
2. With NOT EXISTS:
select distinct s.amount
from sales s
where not exists (
select 1 from sales
where amount > s.amount
)
1 Comment
Luis Diego Arias
It Works, Thanks a lot!
SELECT TOP 1 amount FROM Sales ORDER BY amount DESC