Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Hi All,I get a result after execution of my query:ID Value5 2006 191I want to write a general query to select the ID corresponding to lesser value. Can anybody please help me?Thanks,Mavericky
mavericky
Posting Yak Master
117 Posts
Posted - 2011-09-28 : 18:57:59
Table Name is Emp.Fields are ID and Value.
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts
Posted - 2011-09-28 : 19:08:20
You can use the min function like this:
select TOP 1 ID from Emp group by ID order by MIN(value);
If you are on SQL 2005 or higher, it may be more efficient to use row_number function like this:
with cte as( select *, ROW_NUMBER() OVER (ORDER BY value) RN from Emp )select ID from cte where RN = 1