I have a table like this
id | name
------+-----------
1 | aaa
5 | aaa
2 | bbb
4 | bbb
10 | bbb
7 | ccc
9 | ccc
In my Windows Forms app, I need to "find next" button
I use this query
select
min(name)
from
table
where
[name] >='@name'
but it always returns "aaa"
and
select
min(name)
from
table
where
[name] >'@name'
this does not return other id's
select top 1
[name],[id]
from [table]
where ( [name] = @name
and [id] > @id
)
or [name] > @name
order by [name],[id]
or
lead
looks on the next row when the rows are ordered by the order by
expressions within the over
clause.
select [name],[next_id] as [id]
from (select [id],[name],lead([id]) over (order by [name],@id) as [next_id]
from [table]
) t
where [name] = @name
and [id] = @id