2017-05-10 68 views
0
select * 
from [dbo].[Revenue_PL_S_F_M] rsfm 
    full join [dbo].[Expense_Misc_Variable] emv 
    on emv.FISCYR = rsfm.FISCYR 
    and emv.FISCPER = rsfm.FISCPER 
    and emv.Warehouse = rsfm.Warehouse 
    and emv.Customer = rsfm.Customer 
where RSFM.FISCYR = 2017 
    and RSFM.FISCPER = 1 
    and RSFM.GLGroup = 'M' 

emv有3行,1沒有匹配的rsfm.customer。這行不會被添加到視圖中。完全外部連接無法正常工作

在此先感謝 格里

+2

你在哪裏阻止它被包括在內。 –

+0

好的,還有另一種方法來獲得我需要的東西嗎? –

回答

1

where制約着你的加入。將where移動到加入條件,或使用派生表rsfm

select * 
from [dbo].[Revenue_pl_S_F_M] rsfm 
    full join [dbo].[Expense_Misc_Variable] emv 
    on emv.fiscyr = rsfm.fiscyr 
    and emv.fiscper = rsfm.fiscper 
    and emv.Warehouse = rsfm.Warehouse 
    and emv.Customer = rsfm.Customer 
    and rsfm.fiscyr = 2017 
    and rsfm.fiscper = 1 
    and rsfm.glgroup = 'M' 

select * 
from (
    select * 
    from [dbo].[Revenue_pl_S_F_M] 
    where fiscyr = 2017 
    and fiscper = 1 
    and glgroup = 'M' 
    ) rsfm 
    full join [dbo].[Expense_Misc_Variable] emv 
    on emv.fiscyr = rsfm.fiscyr 
    and emv.fiscper = rsfm.fiscper 
    and emv.Warehouse = rsfm.Warehouse 
    and emv.Customer = rsfm.Customer 
0

完全外連接是相當棘手,尤其是當它涉及到過濾。

我懷疑你可能想:

select * 
from [dbo].[Revenue_PL_S_F_M] rsfm full join 
    [dbo].[Expense_Misc_Variable] emv 
     on emv.FISCYR = rsfm.FISCYR and 
     emv.FISCPER = rsfm.FISCPER and 
     emv.Warehouse = rsfm.Warehouse and 
     emv.Customer = rsfm.Customer 
where coalesce(RSFM.FISCYR, emv.FISCYR) = 2017 and 
     coalesce(RSFM.FISCPER, emv.FISCPER) = 1 and 
     coalesce(RSFM.GLGroup, emv.GLGroup) = 'M'; 

這將返回表之間的匹配時,至少有一個「匹配」排在兩個表中需要的條件相匹配。

我的猜測是,儘管如此,完整的外連接可能不是您遇到的任何問題的最佳方法。你可以考慮用適當的數據樣本和期望的結果來詢問另一個問題。