[英]Query left join without all the right rows from B table
I have 2 tables, A and B. I need all columns from A + 1 column from B in my select. 我有2个表,A和B。我需要选择A中的所有列+ B中的1列。 Unfortunately, B has multiples rows(all identicals) for 1 row in A on the join condition.
不幸的是,在联接条件下,B在A中有1行具有多行(全部相同)。
I tried but I can't isolate one row in A for one row in B with left join for example while keeping my select. 我试过了,但是例如在保持选择的同时,我无法通过左连接将A中的一行与B中的一行隔离。
How can I do this query ? 我该如何查询? Query in ORACLE SQL
在ORACLE SQL中查询
Thanks in advance. 提前致谢。
This is a good use for outer apply
. 这是
outer apply
的好用途。 The structure of the query looks like this: 查询的结构如下所示:
select a.*, b.col
from a outer apply
(select top 1 b.col
from b
where b.? = a.?
) b;
Normally, you would only use top 1
with order by
. 通常,您只会将
top 1
与order by
。 In this case, it doesn't seem to make a difference which row you choose. 在这种情况下,选择哪一行似乎没有什么不同。
You can group by
on all columns from A
, and then use an aggregate (like max
or min
) to pick any of the identical B values: 您可以对
A
所有列进行group by
,然后使用汇总(例如max
或min
)来选择任何相同的B值:
select a.*
, b.min_col1
from TableA a
left join
(
select a_id
, min(col1) as min_col1
from TableB
group by
a_id
) b
on b.a_id = a.id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.