inner-joinlimitsybasesap-ase

How to limit an inner join in Sybase?


How can I limit an inner join or a subquery that it only selects one row? As it seems I can't use 'top 1' in my Sybase version (Sybase version: Adaptive Server Enterprise/15.5/EBF 19902) in subqueries.

Example

select * from A a
inner join B b on a.id = b.Aid

whereat table B has two records linked to table A (same Aid). But I'd like to join only one of these records.

I tried to replace the inner join with a subquery and using top 1, but this is not allowed.


Solution

  • I found a solution here: https://www.periscopedata.com/blog/4-ways-to-join-only-the-first-row-in-sql.html

    select * from A a
    inner join (select * from B b where b.Aid in (select min(Aid) from B group by Aid) ) 
    as b on b.Aid = a.id