Sql server SQL与if条件的复杂联接

Sql server SQL与if条件的复杂联接,sql-server,join,Sql Server,Join,我正在写一个长查询来收集一些数据。下面是一个模拟数据: 表1: a|b|c|d|e 1|1|2|2|134 表2: a2、b2、c2、d2、e2是一个复合键 a2|b2|c2|d2|e2 |f2 |ax2|bx2|cx2|dx2|ex2 1 |1 |2 |2 |134 |Orange|1 |1 |2 |2 |155 1 |1 |2 |2 |155 |Apple |Null|Null|Null|Null|Null 我的问题是这样的: Select * from

我正在写一个长查询来收集一些数据。下面是一个模拟数据:

表1:

a|b|c|d|e   
1|1|2|2|134  
表2:
a2、b2、c2、d2、e2是一个复合键

a2|b2|c2|d2|e2  |f2    |ax2|bx2|cx2|dx2|ex2  
1 |1 |2 |2 |134 |Orange|1  |1  |2  |2  |155  
1 |1 |2 |2 |155 |Apple |Null|Null|Null|Null|Null
我的问题是这样的:

Select * from Table1  
inner join  
Table2 on Table1.a=Table2.a2 and Table1.b=Table2.b2 and Table1.c=Table2.c2 and Table1.d=Table2.d2 and Table1.e=Table2.e2 
这给了我

橙色的

我需要的答案是

苹果

表2非常混乱,所以我要做的是从表1中获取a、b、c、d、e,然后将其插入表2中,获取ex2值,再次运行表2,以ex2替换e2,同时保持a2、b2、c2、d2不变,从而获得Apple

就像我提到的,这有点复杂,所以如果你需要的话,请询问更多细节。我试着尽可能多地给予

我也尝试过这个(仍然没有乐趣):


只需将另一个连接作为左连接添加到查询中,以遍历额外的级别,并使用coalesce显示最低级别(如果存在),或者显示下一个最低级别(如果不存在)

SELECT Coalesce(C.F2, B.F2) as F2
FROM Table1 A
LEFT JOIN TABLE2 B
  on A.a= b.a2
 and A.B = B.B2 
 and A.C = B.C2
 and A.D = B.D2
 and A.E = B.E2
LEFT JOIN TABLE3 C
  on B.Ax2 = C.A2
 and B.Bx2 = C.B2
 and B.Cx2 = C.c2
 and B.Dx2 = C.D2
 and B.Ex2 = C.E2

只需将另一个连接作为左连接添加到查询中,以遍历额外的级别,并使用coalesce显示最低级别(如果存在),或者显示下一个最低级别(如果不存在)

SELECT Coalesce(C.F2, B.F2) as F2
FROM Table1 A
LEFT JOIN TABLE2 B
  on A.a= b.a2
 and A.B = B.B2 
 and A.C = B.C2
 and A.D = B.D2
 and A.E = B.E2
LEFT JOIN TABLE3 C
  on B.Ax2 = C.A2
 and B.Bx2 = C.B2
 and B.Cx2 = C.c2
 and B.Dx2 = C.D2
 and B.Ex2 = C.E2

下面是一个使用临时表的示例

DROP TABLE IF EXISTS #Table1;
CREATE TABLE #Table1 (
    a int not null,
    b int not null,
    c int not null,
    d int not null,
    e int not null,
);
INSERT INTO #Table1 VALUES (1, 1, 2, 2, 134);

DROP TABLE IF EXISTS #Table2;
CREATE TABLE #Table2 (
    a2 int not null,
    b2 int not null,
    c2 int not null,
    d2 int not null,
    e2 int not null,
    f2 nvarchar(10) not null,
    ax2 int null,
    bx2 int null,
    cx2 int null,
    dx2 int null,
    ex2 int null,
    CONSTRAINT PK_Table2 PRIMARY KEY (a2, b2, c2, d2, e2),
);
INSERT INTO #Table2 VALUES
    (1, 1, 2, 2, 134, 'Orange', 1, 1, 2, 2, 155),
    (1, 1, 2, 2, 155, 'Apple', null, null, null, null, null);

SELECT Branch.a2
    , Branch.b2
    , Branch.c2
    , Branch.d2
    , Leaf.e2
    , Leaf.f2
FROM #Table1 AS Root
INNER JOIN #Table2 AS Branch
    ON Root.a = Branch.a2
    AND Root.b = Branch.b2
    AND Root.c = Branch.c2
    AND Root.d = Branch.d2
    AND Root.e = Branch.e2
INNER JOIN #Table2 AS Leaf
    ON Branch.ex2 = Leaf.e2;
结果是

+---------------------+
|a2|b2|c2|d2|e2 |f2   |
+---------------------+
| 1| 1| 2| 2|155|Apple|
+---------------------+

下面是一个使用临时表的示例

DROP TABLE IF EXISTS #Table1;
CREATE TABLE #Table1 (
    a int not null,
    b int not null,
    c int not null,
    d int not null,
    e int not null,
);
INSERT INTO #Table1 VALUES (1, 1, 2, 2, 134);

DROP TABLE IF EXISTS #Table2;
CREATE TABLE #Table2 (
    a2 int not null,
    b2 int not null,
    c2 int not null,
    d2 int not null,
    e2 int not null,
    f2 nvarchar(10) not null,
    ax2 int null,
    bx2 int null,
    cx2 int null,
    dx2 int null,
    ex2 int null,
    CONSTRAINT PK_Table2 PRIMARY KEY (a2, b2, c2, d2, e2),
);
INSERT INTO #Table2 VALUES
    (1, 1, 2, 2, 134, 'Orange', 1, 1, 2, 2, 155),
    (1, 1, 2, 2, 155, 'Apple', null, null, null, null, null);

SELECT Branch.a2
    , Branch.b2
    , Branch.c2
    , Branch.d2
    , Leaf.e2
    , Leaf.f2
FROM #Table1 AS Root
INNER JOIN #Table2 AS Branch
    ON Root.a = Branch.a2
    AND Root.b = Branch.b2
    AND Root.c = Branch.c2
    AND Root.d = Branch.d2
    AND Root.e = Branch.e2
INNER JOIN #Table2 AS Leaf
    ON Branch.ex2 = Leaf.e2;
结果是

+---------------------+
|a2|b2|c2|d2|e2 |f2   |
+---------------------+
| 1| 1| 2| 2|155|Apple|
+---------------------+


您需要给出实际的表结构和实际的查询,否则将很难提供帮助。表1和表2中的E2都是134。为什么要给你155?哦,这是一种等级制度。层次结构总是只有1级吗?如果是这样,请在表2中再次添加一个连接(如果可以是n个级别),然后需要递归cte或用于xml路径来遍历层次结构。@xQbert在某些情况下,答案是Apple/Orange。如果使用表2中的e2,表1将显示橙色。如果你也想要苹果,那么你需要得到ex2的值,并将其插入e2以得到苹果。所以,是的,这是一种层次结构。根据您的示例数据,答案应该是橙色。@xQbert是的,它总是只有一个级别,与ax2、bx2、…EX2一样。您需要给出实际的表结构和实际的查询,否则将很难提供帮助。它应该会让您在表1和表2中看到橙色E2是134。为什么要给你155?哦,这是一种等级制度。层次结构总是只有1级吗?如果是这样,请在表2中再次添加一个连接(如果可以是n个级别),然后需要递归cte或用于xml路径来遍历层次结构。@xQbert在某些情况下,答案是Apple/Orange。如果使用表2中的e2,表1将显示橙色。如果你也想要苹果,那么你需要得到ex2的值,并将其插入e2以得到苹果。因此,是的,这是一种层次结构。根据您的样本数据,答案应该是橙色。@xQbert是的,它总是只有一个级别,因为ax2、bx2……Ex2完成了这项工作!这就成功了!您的查询也很有效,但xQbert首先得到了它。非常感谢!左连接而不是内部连接,以防没有叶子?就像155号的情况一样?当然,如果这是你的意图的话。从这个问题上讲有点难。您的查询也很有效,但xQbert首先得到了它。非常感谢!左连接而不是内部连接,以防没有叶子?就像155号的情况一样?当然,如果这是你的意图的话。从这个问题上讲有点难。