Home > Software engineering >  One to many join table
One to many join table

Time:07-29

I have a table with a list of account numbers.

I am joining this table with one that has account owner's.

Tha question is that some account may have more that 1 owner, so my query return some duplicated results.

Is there a function to return the "first" matching results and ignore the second owner?

CodePudding user response:

I'm making guesses as to what your schema looks like as you did not share it.

select  *
from    Accounts a
        join Users u on a.Id = u.AccountId
where   u.id in (select min(x.id) from Users x group by x.AccountId)

You could get better answers if you share your schema (at least the relevant parts) and what SQL Engine you're using as some have special features to make this even more straightforward.

  •  Tags:  
  • sql
  • Related