我试图将Item.ItemLookupCode
更新为
ItemClass.ItemLookupCode +
MatrixAttributeDisplayOrder.Code(Dimension 1) +
MatrixAttributeDisplayOrder.Code(Dimension 2) +
MatrixAttributeDisplayOrder.Code(Dimension 3)
哪会返回Item.ItemLookupCode = F06555SND36LG
使用下表。非常感谢任何帮助,谢谢:
db.Item
:
ID | ItemLookupCode
-----------------------
68883 | 4344110101
db.ItemClassComponent
:
ID |ItemClassID | ItemID | Detail1 | Detail2 | Detail3
----------------------------------------------------------
68883 | 2566 | 68905 | Sand | 36 | Long
db.MatrixAttributeDisplayOrder
:
ID | ItemClassID | Dimension | Attribute | Code
---------------------------------------------------
19769 | 2566 | 2 | 36 | 36
19774 | 2566 | 3 | Long | LG
47708 | 2566 | 1 | Sand | Snd
1200 | 88 | 1 | 32 Short | 32S
4272 | 366 | 3 | Long | LNG
3207 | 266 | 3 | Short | SH
19767 | 2566 | 1 | Navy | NVY
19768 | 2566 | 2 | 34 | 34
19772 | 2566 | 3 | Short | SH
db.ItemClass
:
ID | ItemLookupCode
----------------------
2566 | F06555
答案 0 :(得分:1)
此查询应该执行您想要的操作,如果要对其进行测试,您可以单独运行select查询以返回返回的值。使用SQL 2012及更高版本,您可以使用concat()
将值合并到一个字符串中,如果您将较低版本更改为现在已注释掉的行。
update Item
set ItemLookupCode = LookupCodes.ItemLookupCode
from Item i
inner join
(
select
icc.id,
-- SQL 2012+
ItemLookupCode = upper(concat(ic.ItemLookupCode, m1.code, m2.code, m3.code))
-- pre SQL 2012
-- ItemLookupCode = upper(ic.ItemLookupCode + m1.code + m2.code + m3.code)
from ItemClassComponent icc
inner join MatrixAttributeDisplayOrder m1
on icc.ItemClassID = m1.ItemClassID and m1.Dimension = 1
inner join MatrixAttributeDisplayOrder m2
on icc.ItemClassID = m2.ItemClassID and m2.Dimension = 2
inner join MatrixAttributeDisplayOrder m3
on icc.ItemClassID = m3.ItemClassID and m3.Dimension = 3
inner join ItemClass ic
on icc.ItemClassID = ic.ID
) LookupCodes on LookupCodes.ID = i.ID
where i.ID = 68883;
另一个版本的查询包含了评论中提出的其他要求:
select
icc.id,
-- SQL 2012+
ItemLookupCode = upper(concat(ic.ItemLookupCode, m1.code, m2.code, m3.code))
-- pre SQL 2012
-- ItemLookupCode = upper(ic.ItemLookupCode + m1.code + m2.code + m3.code)
from ItemClassComponent icc
inner join MatrixAttributeDisplayOrder m1
on icc.ItemClassID = m1.ItemClassID
and icc.Detail1 = m1.Attribute
and m1.Dimension = 1
inner join MatrixAttributeDisplayOrder m2
on icc.ItemClassID = m2.ItemClassID
and icc.Detail2 = m2.Attribute
and m2.Dimension = 2
inner join MatrixAttributeDisplayOrder m3
on icc.ItemClassID = m3.ItemClassID
and icc.Detail3 = m3.Attribute
and m3.Dimension = 3
inner join ItemClass ic
on icc.ItemClassID = ic.ID
答案 1 :(得分:0)
我不确定您想要连接哪些字段,但这里是代码字段。它将整个表作为一组来完成。
UPDATE item SET ItemLookupCode = Result
FROM Item INNER JOIN (
SELECT icc.id, CONCAT(ic.ItemLookupCode, M1.Code1, m2.Code2,m3.Code3) AS Result
FROM ItemClassComponent icc
LEFT OUTER JOIN ItemClass ic on icc.ItemClassID = ic.ID
CROSS APPLY (SELECT Code AS Code1 FROM MatrixAttributeDisplayOrder D1 WHERE D1.ItemClassID = icc.ItemClassID AND D1.Dimension = 1 ) M1
CROSS APPLY (SELECT Code AS Code2 FROM MatrixAttributeDisplayOrder D1 WHERE D1.ItemClassID = icc.ItemClassID AND D1.Dimension = 2 ) M2
CROSS APPLY (SELECT Code AS Code3 FROM MatrixAttributeDisplayOrder D1 WHERE D1.ItemClassID = icc.ItemClassID AND D1.Dimension = 3 ) M3
) T1 ON item.ID = t1.ID