Subject RE: [firebird-support] Get ID of record with minsort
Author Svein Erling Tysvær

>I have a table with one ID-Column and one Sort-Column.
>
>I need the ID from the record with the lowest sort-number.
>Sortnumbers are not unique (can be doubled).
>
>Can I do this with one SQL-Statement?

If you want one row returned:

 

Select ID_column

From <Table>

Order by Sort_Column

Rows 1

 

If you want all rows returned:

 

Select ID_column

From <Table> t1

Where not exists(select * from <Table> t2 where t1.Sort > t2.Sort)

 

Alternatively, you could use

 

With tmp(Sort) as

(select min(Sort) from <table>)

Select t.ID_Column

From tmp

Join <table> t on tmp.Sort = t.Sort

 

Set