tags:

views:

49

answers:

5

Having this selection:

id IDSLOT  N_UM
------------------------
1  1  6
2  6  2
3  2  1
4  4  1
5  5  1
6  8  1
7  3  1
8  7  1
9  9  1
10  10  0

I would like to get the row (only one) which has the minimun value of N_UM, in this case the row with id=10 (10 0).

+4  A: 

I'd try this:

SELECT TOP 1 *
FROM TABLE1
ORDER BY N_UM

(using SQL Server)

Adam V
+1 Beat me by 4 seconds. We need to know RDBMS though as might be `limit` or something else.
Martin Smith
@Martin Smith: true true. I use SQL Server, so I just answered it that way. Should probably mark it as such. :)
Adam V
+1  A: 

Try this -

 select top 1 * from table where N_UM = (select min(N_UM) from table);
Sachin Shanbhag
plus, he only wanted one row.
Sage
+ `Limit 1` if you only want 1 row (like asked).
Chouchenos
Agreed, agreed - Edited my answer to satisfy all comments.
Sachin Shanbhag
Still, Adam V and my variant is more pure: one select, no filtering.
Andrejs Cainikovs
+2  A: 
select id,IDSLOT,N_UM from table where N_UM = (select min(N_UM) from table));
bunting
Why `in` rather than `=`? This can cause performance issues in MySQL.
Martin Smith
thanks, corrected
bunting
Wrong : 1. will return multiple rows, 2. two selects for such a simple task is not an option
Andrejs Cainikovs
+1  A: 

Here is one approach

Create table #t (
id int,
IDSLOT int,
N_UM int
)
insert into #t ( id, idslot, n_um )
VALUES (1, 1, 6),
 (2,6,2),
 (3,2,1),
 (4,4,1),
 (5,5,1),
 (6,8,1),
 (7,3,1),
 (8,7,1),
 (9,9,1),
 (10, 10, 0)

 select Top 1 *
 from #t
 Where N_UM = ( select MIN(n_um) from #t )
Sage
+2  A: 
select * from TABLE_NAME order by COLUMN_NAME limit 1
Andrejs Cainikovs
Don't you mean `order by N_UM`?
egrunin
This is a generic example. Substitute the table and column names with whatever you need.
Andrejs Cainikovs