简体   繁体   English

使用FULL OUTER JOIN

[英]Using FULL OUTER JOIN

I have two views, which contains an ID, Unit and Quantity. 我有两个视图,其中包含一个ID,单位和数量。 One view is for actuals and one for estimates. 一种观点是实际的,另一种观点是估计的。 I want a select statement that will return all rows from both views, for a specific ID, regardless of whether the units are in both views. 我想要一个select语句,该返回语句将针对特定的ID从两个视图返回所有行,而不管单位是否在两个视图中。 I can't seem to get this to work with the FULL OUTER JOIN, as I need to join on the ID and the Unit. 我似乎无法使它与FULL OUTER JOIN一起使用,因为我需要加入ID和Unit。

在此处输入图片说明

In this example vVMVoyEstTotalBLQuantitySrch contains an entry for CBM, which is not in vVMVoyBLQuantitySrch. 在此示例中,vVMVoyEstTotalBLQuantitySrch包含CBM的条目,该条目不在vVMVoyBLQuantitySrch中。 I still want all the lines, eg: 我仍然想要所有的行,例如:

在此处输入图片说明

It could also be the other way around, eg that vVMVoyEstTotalBLQuantitySrch does not contain a specific unit, but vVMVoyBLQuantitySrch does, and again I want all rows. 也可能是另一种方式,例如vVMVoyEstTotalBLQuantitySrch不包含特定单位,但是vVMVoyBLQuantitySrch包含特定单位,并且我再次希望所有行。 If it is in at least one of the views, return it. 如果在至少一个视图中,则将其返回。

I have tried some code snippets from the internet, using COALESCE on both ID and Unit, but then I only get the missing entry (CBM), and not all. 我已经在Internet上尝试了一些代码段,在ID和Unit上都使用了COALESCE,但是后来我只得到了缺少的条目(CBM),而并非全部。

Please help! 请帮忙!

Does your attempt look like this? 您的尝试看起来像这样吗?

select . . .
from view1 v1 full outer join
     view2 v2
     on v1.id = v2.id
where 15506 in (v1.id, v2.id);

The trick is that you have to check both ids, because the full outer join is likely to produce NULL values. 诀窍是您必须检查两个id,因为full outer join可能会产生NULL值。

try Below query 试试下面的查询

SELECT ISNULL(q1.Id,q2.Id) As Id, 
ISNULL(q1.estLoadUnit,q2.ActLoadUnit) As Units,
q1.EstTotalLoaded,
 q2.ActTotalLoadded
FROM vVMVoyEstTotalBLQuantitySrch  q1
FULL OUTER JOIN vVMVoyBLQuantitySrch q2 ON q1.Id = q2.Id
                     AND q2.actLoadUnit = q1.EstLoadUnit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM