Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何使用多个条件执行左联接?_Sql_Join_Left Join_Relationship - Fatal编程技术网

Sql 如何使用多个条件执行左联接?

Sql 如何使用多个条件执行左联接?,sql,join,left-join,relationship,Sql,Join,Left Join,Relationship,我有一个带值的表和一个带标识符的表,我正在尝试将标识符表左键连接到值表 问题是在标识符表中,它不是1:1匹配 首先,我需要左连接以按成本中心匹配所有内容,但对于附加了特定帐户类别的连接,我只希望在成本中心内映射这些帐户类别 如何构造join语句 目前,我已将其设置为: `SELECT * FROM [TABLE 1] t1 LEFT JOIN [TABLE 2] t2 ON t1.[Cost Center] = t2.[Cost Center] AND **NEED SOMETHING TO

我有一个带值的表和一个带标识符的表,我正在尝试将标识符表左键连接到值表

问题是在标识符表中,它不是1:1匹配

首先,我需要左连接以按成本中心匹配所有内容,但对于附加了特定帐户类别的连接,我只希望在成本中心内映射这些帐户类别

如何构造join语句

目前,我已将其设置为:

`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`

您可以在连接或where中添加条件

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


如果我正确理解了问题,这应该是可行的:

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]

如果我正确理解了您的问题,这里的诀窍是您需要2个
LEFT JOIN
语句和一个
ISNULL
语句。下面,第二个
左联接只查看没有AccountCategory的行。这将生成表3。此解决方案假定Table1的AccountCategory不能为null/空

设置:

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

您可以共享您尝试为此示例输入获取的输出吗?类似于:
SELECT*FROM[Table1]在[Table1]上左键联接[Table2]。[Cost Center]=[Table2]。[Cost Center]和([Table1].[Account Category]=[Table2].[Account Category]或([Table1].[Account Category]为空,而[Table2].[Account Category]为空))
,也许?表3是期望的输出吗?是的,表3是我希望最终产品的样子@戴蒙德,那没用。它仍然不能捕获所有内容,这将给出不同的结果集。把它放在where使它基本上成为一种内部连接。这不起作用,因为我想把一些成本中心全部包括进去,而不考虑账户类别。这将它限制在有匹配的地方。但由于它是一个左连接,它将包括表1中的所有成本中心;它不仅限于匹配,这将是一个内部连接
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