[英]Joining temp tables with different structure
I have 2 temp tables.我有 2 个临时表。 One has data column wise.一个有数据列。
OrderCode OrderType
ABC 1000
One has data row wise一个有数据行明智
FieldName Value
ORDER_CODE ABC
ORDER_TYPE 2000
I need to write a query that essentially joins both of them and updates the value if it exists in table 2.我需要编写一个查询,该查询基本上将它们连接起来并更新值(如果它存在于表 2 中)。
Recordset result needs to be记录集结果需要
OrderCode OrderType
ABC 2000
EDIT-编辑-
If there are 2 orders in table 1 with the same order code:如果表 1 中有 2 个订单具有相同的订单代码:
OrderCode OrderType
ABC 1000
ABC 5000
the result set will need to be结果集需要是
OrderCode OrderType
ABC 2000
ABC 2000
To select the rowset:到 select 行集:
SELECT tt1.ordercode, tt2_type.Value
FROM Table2 AS tt2_code
JOIN Table1 tt1
ON tt1.orderCode = tt2_code.value
JOIN
Table2 AS tt2_type
ON tt2_type.fieldName = 'ORDER_TYPE'
WHERE tt2_code.fieldName = 'ORDER_CODE'
To update Table1
based on values from Table2
:要根据Table2
中的值更新Table1
:
WITH q AS
(
SELECT tt1.ordercode, tt2_type.Value
FROM Table2 AS tt2_code
JOIN Table1 tt1
ON tt1.orderCode = tt2_code.value
JOIN
Table2 AS tt2_type
ON tt2_type.fieldName = 'ORDER_TYPE'
WHERE tt2_code.fieldName = 'ORDER_CODE'
)
UPDATE q
SET oldvalue = newvalue
This is one of the cases where JOIN
-less syntax is more legible.这是JOIN
-less 语法更清晰的情况之一。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.