与桌子联接,但仅在特定条件下

时间:2019-02-19 09:30:46

标签: sql-server sql-server-2014

我需要添加一个联接,但是只有在满足特定条件时才需要。

让我举例说明。

我有这3张桌子

Table_I

I_ID    Value   Ischecked   Region  
----    -----   ---------   ------  
1       A       0           N1  
2       B       1           N1  
3       C       0           N2  

Table_PB

PB_ID   Region  Code    
-----   ------  ----    
1       N1      A1  
2       N1      A2  
3       N1      A3  
4       N2      C1  

表D

D_ID    I_ID    PB_ID   
----    ----    -----   
1       1       1   
2       1       2   
3       3       1   
4       3       4   

我想要的结果是这个

I_ID    Value   IsChecked   Region  PB_ID   Code    
----    -----   ---------   ------  -----   ----    
1       A       0           N1      1       A1  
1       A       0           N1      2       A2  
2       B       1           N1      null    A1  
2       B       1           N1      null    A2  
2       B       1           N1      null    A3  
3       C       0           N2      1       A1  
3       C       0           N2      4       C1  

我怎么得到这个结果?

好吧,我从Table_I的所有行开始,然后与Table_D一起加入,最后到Table_PB加入
要注意的是,如果在Table_DIsChecked is 1中找不到行,那么我需要在Table_PB上进行额外的联接,这将添加所有与{{ {1}}

我希望我的意思很清楚。

所以我现在得到的是这个

Region

这会产生结果

Table_I

将其与我想要的结果进行比较时,您可以看到I_ID = 2的行的差异

这可以做到吗?

2 个答案:

答案 0 :(得分:1)

尝试使用UNION ALL在2组之间分离逻辑:

-- Records from I that exists on D
SELECT
    I.I_ID,
    I.Value,
    I.Ischecked,
    I.Region,
    D.PB_ID,
    P.Code
FROM
    @Table_I AS I
    INNER JOIN @Table_D AS D ON I.I_ID = D.I_ID
    INNER JOIN @Table_PB AS P ON D.PB_ID = P.PB_ID

UNION ALL

-- Records from I that don't exist on D and are checked
SELECT
    I.I_ID,
    I.Value,
    I.Ischecked,
    I.Region,
    PB_ID = NULL, --P.PB_ID,
    P.Code
FROM
    @Table_I AS I
    INNER JOIN @Table_PB AS P ON I.Region = P.Region
WHERE
    I.Ischecked = 1 AND
    NOT EXISTS (SELECT 'no record on D' FROM @Table_D AS D WHERE D.I_ID = I.I_ID)

ORDER BY
    1

结果:

I_ID    Value   Ischecked   Region  PB_ID   Code
1       A       0           N1      1       A1
1       A       0           N1      2       A2
2       B       1           N1      NULL    A1
2       B       1           N1      NULL    A2
2       B       1           N1      NULL    A3
3       C       0           N2      4       C1
3       C       0           N2      1       A1

我要在第二组上强制使用NULL,因为它符合您期望的结果,但是您可以在此处显示PB_ID表中的@Table_PB(已注释)在代码上)。

答案 1 :(得分:1)

您可以使用CTE

declare @Table_I table (I_ID int, Value varchar(10), Ischecked bit, Region varchar(2))
insert into @Table_I values (1, 'A', 0, 'N1'), (2, 'B', 1, 'N1'), (3, 'C', 0, 'N2')

declare @Table_PB table (PB_ID int, Region varchar(2), Code varchar(2))
insert into @Table_PB values (1, 'N1', 'A1'), (2, 'N1', 'A2'), (3, 'N1', 'A3'), (4, 'N2', 'C1')

declare @Table_D table (D_ID int, I_ID int, PB_ID int)
insert into @Table_D values (1, 1, 1), (2, 1, 2), (3, 3, 1), (4, 3, 4)

;
WITH cte AS (
    SELECT i.I_ID, count(D.PB_ID) as CountD
    FROM @Table_I i
    LEFT JOIN @Table_D D ON i.I_ID = d.I_ID 
    GROUP BY i.I_ID
)
SELECT 
    i.I_ID, i.Value, i.IsChecked, i.Region, d.PB_ID, pb.Code
FROM CTE c
  join @Table_I i on i.I_ID = c.i_id
  left join @Table_D d on i.I_ID = d.I_ID
  left join @Table_PB pb on (d.PB_ID = pb.PB_ID) OR (c.CountD = 0 AND i.isChecked = 1 AND i.Region = pb.Region)