2016-03-08 70 views
0

我有值的表和1標識符和我想LEFT JOIN識別符表中的值表。如何使用多個條件執行左連接?

的問題是,在標識表,它不是1:1匹配。

我需要的左連接到首先匹配由成本中心的一切,但具有連接特定的科目類別的那些,我想只有那些賬戶類的成本中心內進行映射。

Tables

我將如何構建我的連接語句?

目前,我有它設置爲:

`SELECT * FROM [TABLE 1] t1 
LEFT JOIN [TABLE 2] t2 
ON t1.[Cost Center] = t2.[Cost Center] 
AND **NEED SOMETHING TO FILTER OUT SELECTED ACCOUNT CATEGORIES` 
+1

能否請你分享你正在試圖獲得該樣品的輸入輸出? – Mureinik

+0

是這樣的:'SELECT * FROM [表1] LEFT JOIN [表2]在[TABLE1] [成本中心] = [表2] [成本中心] AND([表1] [科目類別] = [表2]。。。。 [Account Category] ​​OR([Table1]。[Account Category] ​​IS NULL AND [Table2]。[Account Category] ​​IS NULL))''也許? –

+0

是「表3」所需的輸出? – Jayvee

回答

0

您可以添加在加入或地方的條件。

SELECT * FROM [TABLE 1] t1 
LEFT JOIN [TABLE 2] t2 
ON t1.[Cost Center] = t2.[Cost Center] 
AND t1.Category = 'bleh' 

SELECT * FROM [TABLE 1] t1 
LEFT JOIN [TABLE 2] t2 
ON t1.[Cost Center] = t2.[Cost Center] 
WHERE t1.Category = 'bleh' 
+1

,這會給出不同的結果集。把它放在那裏使它基本上是一種內部連接。 –

0

,如果我的理解這個問題正確的,這應該工作:

SELECT t1.[Cost Center], t1.[Account Category],t2.[Flag] 
FROM [TABLE 1] t1 
LEFT JOIN [TABLE 2] t2 
ON t1.[Cost Center] = t2.[Cost Center] 
AND t1.[Account Category] = t2.[Account Category] 
+0

這不起作用,因爲有些成本中心我希望全部包括在內,而不管帳戶類別如何。這限制了它只在有匹配的地方。 –

+0

但由於它是左連接,它將包括表1中的所有成本中心;它不是限制只匹配,這將是一個內部聯接 – Jayvee

0

如果我正確理解你的問題,這裏的技巧是,你需要2點LEFT JOIN聲明並在選擇ISNULL。下面,第二個LEFT JOIN只查看沒有AccountCategory的行。這會產生你的Table3。該解決方案假設Table1的AccountCategory不能爲空/空。

設置:

DECLARE @Table1 TABLE 
(
    CostCenter INT, 
    AccountCategory VARCHAR(10) 
) 

DECLARE @Table2 TABLE 
(
    CostCenter INT, 
    AccountCategory VARCHAR(10), 
    Flag VARCHAR(10) 
) 

INSERT INTO @Table1 (CostCenter, AccountCategory) VALUES (8601, 'ABC1'); 
INSERT INTO @Table1 (CostCenter, AccountCategory) VALUES (8601, 'ABC1'); 
INSERT INTO @Table1 (CostCenter, AccountCategory) VALUES (8601, 'ABC2'); 
INSERT INTO @Table1 (CostCenter, AccountCategory) VALUES (8601, 'ABC3'); 
INSERT INTO @Table1 (CostCenter, AccountCategory) VALUES (8601, 'ABC4'); 

INSERT INTO @Table1 (CostCenter, AccountCategory) VALUES (8602, 'ABC1'); 
INSERT INTO @Table1 (CostCenter, AccountCategory) VALUES (8602, 'ABC1'); 
INSERT INTO @Table1 (CostCenter, AccountCategory) VALUES (8602, 'ABC2'); 
INSERT INTO @Table1 (CostCenter, AccountCategory) VALUES (8602, 'ABC3'); 
INSERT INTO @Table1 (CostCenter, AccountCategory) VALUES (8602, 'ABC4'); 

INSERT INTO @Table1 (CostCenter, AccountCategory) VALUES (8603, 'ABC1'); 
INSERT INTO @Table1 (CostCenter, AccountCategory) VALUES (8603, 'ABC1'); 
INSERT INTO @Table1 (CostCenter, AccountCategory) VALUES (8603, 'ABC2'); 
INSERT INTO @Table1 (CostCenter, AccountCategory) VALUES (8603, 'ABC3'); 
INSERT INTO @Table1 (CostCenter, AccountCategory) VALUES (8603, 'ABC4'); 

INSERT into @Table2 (CostCenter, AccountCategory, Flag) VALUES (8601, 'ABC1', 'Yes'); 
INSERT into @Table2 (CostCenter, AccountCategory, Flag) VALUES (8602, null, 'Yes'); 
INSERT into @Table2 (CostCenter, AccountCategory, Flag) VALUES (8603, null, 'Yes'); 

查詢:

SELECT t1.CostCenter, t1.AccountCategory, ISNULL(t2a.Flag, t2b.Flag) AS Flag 
FROM @Table1 t1 
LEFT JOIN @Table2 t2a ON ISNULL(t2a.AccountCategory,'') = t1.AccountCategory AND t1.CostCenter = t2a.CostCenter 
LEFT JOIN @Table2 t2b ON ISNULL(t2b.AccountCategory,'') = '' AND t1.CostCenter = t2b.CostCenter 

結果:

CostCenter AccountCategory Flag 
8601 ABC1 Yes 
8601 ABC1 Yes 
8601 ABC2 NULL 
8601 ABC3 NULL 
8601 ABC4 NULL 
8602 ABC1 Yes 
8602 ABC1 Yes 
8602 ABC2 Yes 
8602 ABC3 Yes 
8602 ABC4 Yes 
8603 ABC1 Yes 
8603 ABC1 Yes 
8603 ABC2 Yes 
8603 ABC3 Yes 
8603 ABC4 Yes 
+0

這工作!謝謝! –