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

前端 未结 6 478
孤城傲影
孤城傲影 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:49

    In MySQL you could just GROUP BY ASSIGNMENT and be done. Oracle is more strict and refuses to just choose (in an undefined way) which values of the three rows to choose. That means all returned columns need to be part of GROUP BY or be subject to an aggregat function (COUNT, MIN, MAX...)

    You can of course choose to just don't care and use some aggregat function on the returned columns.

    select TICKET_ID, ASSIGNMENT, MAX(MANAGER_NAME), MAX(USER)
    from T1
    left join T2 on T1.ASSIGNMENT=T2.ASSIGNMENT_GROUP
    group by TICKET_ID, ASSIGNMENT
    

    If you do that I would seriously doubt that you need the JOIN in the first place.

    MySQL could also help with GROUP_CONCAT in the case that you want a string concatenation of group values for a column (humans often like that), but with Oracle that is staggeringly complex.

    Using a subquery as already suggested is an option, look here for an example. It also allows you to sort the subquery before selecting the top row.

提交回复
热议问题