基于多个字段的完整外连接
这是我面临的情况:
我有两个表A和B。如果记录在表A中而不在表B中,则需要将它们添加到表B中。如果记录在表B中而不在表A中,那么它们需要从表 B 中删除。这里的技巧是,它是两个键的混合,这使得
Table A
Operation_Key Part_Key
1 1
1 2
2 1
2 3
Table B
Operation_Key Part_Key Record_Key
1 1 1
2 1 2
2 3 3
2 4 4
我试图获得正确类型的查询的唯一组合,以便返回的结果看起来像
Results
Operation_Key Part_Key Record_Key Action
1 2 NULL Add
2 4 4 Delete
我的查询到目前为止看起来与此类似:
CREATE TABLE #Action_Table
(
Action VARCHAR(6),
Action_Bit INT,
Operation_Key INT,
Record_Key INT,
Part_Key INT
)
INSERT INTO #Action_Table
SELECT
CASE
WHEN WS.Operation_Key IS NULL THEN 'Delete'
WHEN WS.Operation_Key IS NOT NULL THEN 'Add'
END Action,
CASE
WHEN WS.Operation_Key IS NULL THEN '0'
WHEN WS.Operation_Key IS NOT NULL THEN '1'
END Action_Bit,
CASE
WHEN WS.Operation_Key IS NULL THEN WC.Operation_Key
WHEN WS.Operation_Key IS NOT NULL THEN WS.Operation_Key
END Operation_Key,
CASE
WHEN WS.Operation_Key IS NULL THEN WC.Record_Key
WHEN WS.Operation_Key IS NOT NULL THEN NULL
END Workcenter_Component_Key,
CASE
WHEN WS.Operation_Key IS NULL THEN WC.Part_Key
WHEN WS.Operation_Key IS NOT NULL THEN WS.Part_Key
END Part_Key
FROM #WS_Part_Table WS
FULL OUTER JOIN #WC_Part_Table WC
ON WC.Part_Key = WS.Part_Key
AND WC.Operation_Key = WS.Operation_Key
WHERE (WS.Part_Key IS NULL or WC.Part_Key IS NULL) AND (WS.Operation_Key IS NULL or WC.Operation_Key IS NULL)
#WS_Part_Table 和 #WC_Part_Table 都是我使用查询构建的临时表,但我的困境是我必须预查询我感兴趣的操作键,否则我会得到太多结果。
这是我用来创建 #WC_Part_Table 的查询
CREATE TABLE #WC_Part_Table
(
Operation_Key INT,
Record_Key INT,
Part_Key INT
)
-- Workcenter Component Table
INSERT INTO #WC_Part_Table
SELECT
O.Operation_Key,
WC.Record_Key,
WC.Part_Key
FROM Workcenter_Component WC
JOIN Operation O
ON O.Default_Workcenter_Key = WC.Workcenter_Key
/* There is some reason why this next line is needed */
WHERE O.Operation_Key = 23149
Here is the situation that I'm facing:
I have two tables A and B. If records are in table A and not in table B they need to be added to table B. If records are in table B and not in table A, then they need to be removed out of table B. The trick here is that it is the mixture of two keys that makes the unique combination
Table A
Operation_Key Part_Key
1 1
1 2
2 1
2 3
Table B
Operation_Key Part_Key Record_Key
1 1 1
2 1 2
2 3 3
2 4 4
I am trying to get the right type of query so that the results returned look like
Results
Operation_Key Part_Key Record_Key Action
1 2 NULL Add
2 4 4 Delete
The query I have so far looks like similar to this:
CREATE TABLE #Action_Table
(
Action VARCHAR(6),
Action_Bit INT,
Operation_Key INT,
Record_Key INT,
Part_Key INT
)
INSERT INTO #Action_Table
SELECT
CASE
WHEN WS.Operation_Key IS NULL THEN 'Delete'
WHEN WS.Operation_Key IS NOT NULL THEN 'Add'
END Action,
CASE
WHEN WS.Operation_Key IS NULL THEN '0'
WHEN WS.Operation_Key IS NOT NULL THEN '1'
END Action_Bit,
CASE
WHEN WS.Operation_Key IS NULL THEN WC.Operation_Key
WHEN WS.Operation_Key IS NOT NULL THEN WS.Operation_Key
END Operation_Key,
CASE
WHEN WS.Operation_Key IS NULL THEN WC.Record_Key
WHEN WS.Operation_Key IS NOT NULL THEN NULL
END Workcenter_Component_Key,
CASE
WHEN WS.Operation_Key IS NULL THEN WC.Part_Key
WHEN WS.Operation_Key IS NOT NULL THEN WS.Part_Key
END Part_Key
FROM #WS_Part_Table WS
FULL OUTER JOIN #WC_Part_Table WC
ON WC.Part_Key = WS.Part_Key
AND WC.Operation_Key = WS.Operation_Key
WHERE (WS.Part_Key IS NULL or WC.Part_Key IS NULL) AND (WS.Operation_Key IS NULL or WC.Operation_Key IS NULL)
Both the #WS_Part_Table and the #WC_Part_Table are temp tables that I'm constructing using queries, but my dilemma is that I have to PRE-QUERY the #WC_Part_Table query on the operation key that I'm interested in, otherwise I get way too many results.
This is the query that I'm using to create the #WC_Part_Table
CREATE TABLE #WC_Part_Table
(
Operation_Key INT,
Record_Key INT,
Part_Key INT
)
-- Workcenter Component Table
INSERT INTO #WC_Part_Table
SELECT
O.Operation_Key,
WC.Record_Key,
WC.Part_Key
FROM Workcenter_Component WC
JOIN Operation O
ON O.Default_Workcenter_Key = WC.Workcenter_Key
/* There is some reason why this next line is needed */
WHERE O.Operation_Key = 23149
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
执行此操作即可获取您发布的结果:
测试脚本:
输出:
Tyr this to get the results that you have posted:
Test Script:
Output:
添加到 B:
从 B 删除:
Add to B:
Delete from B:
通过巧妙地使用 SQL“COALESCE”运算符,您可以准确地获得您想要的结果表(查看您设置的示例)。如果您使用这样的查询:
...您将得到一个与您的示例完全相同的结果表。
You can get exactly the results table you want (looking at your example set up) by making clever use of the SQL "COALESCE" operator. If you use a query like this:
...you'll get a result table exactly like your example.