How do I limit the number of rows returned by this LEFT JOIN to one?

前端 未结 6 523
孤城傲影
孤城傲影 2021-01-11 17:08

So I think I\'ve seen a solution to this however they are all very complicated queries. I\'m in oracle 11g for reference.

What I have is a simple one to many join w

6条回答
  •  盖世英雄少女心
    2021-01-11 17:56

    You could do something like this.

    SELECT t1.ticket_id, 
           t1.assignment,
           t2.manager_name,
           t2.user
      FROM table1 t1
           LEFT OUTER JOIN (SELECT manager_name,
                                   assignment_group,
                                   user,
                                   row_number() over (partition by assignment_group
                                                        --order by <>
                                                     ) rnk
                              FROM table2) t2
                         ON (    t1.assignment = t2.assignment_group
                             AND t2.rnk = 1 )
    

    This partitions the data in table2 by assignment_group and then arbitrarily ranks them to pull one arbitrary row per assignment_group. If you care which row is returned (or if you want to make the row returned deterministic) you could add an ORDER BY clause to the analytic function.

提交回复
热议问题