I need to get the ID based from what ever the max amount is. Below is giving me an error
select ID from Prog
where Amount = MAX(Amount)
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.
The end result is that I need to get the just the ID as I need to pass it something else that is expecting it.
You need to order by Amount and select 1 record instead…
This takes all the rows in
Prog, orders them in descending order byAmount(in other words, the first sorted row has the highestAmount), then limits the query to select only one row (the one with the highestAmount).Also, subqueries are bad for performance. This code runs on a table with 200k records in half the time as the subquery versions.