问题 :
表格1
CatId - - Type - - Qty
==============================
8 || O || 10
8 || N || 20
8 || U || 30
30 || N || 5
30 || O || 15
30 || NULL || 25
表2
catId -- Old - -New -- Useless -- Other
========================================
8 || 100 || 70 || 140 || 110
30 || 10 || 20 || 30 || 50
结果:用表 1 更新表 2
-------------------------------------------------
catId -- Old -- New -- Useless -- Other
8 || 90 || 50 || 110 || 110
30 || 5 || 5 || 30 || 25
结果应该如何:
表 1 和表 2 有一个共同的列 CatId。
Column of table 1 Type is connects with Table2
AS (Old - O / New - N / Useless - U / Other - NULL)
我想减去 Like table2(Respective O/N/U/Other) = table2(Respective O/N/U/Other) - table1(Type) 并且更喜欢没有循环的解决方案
我试过了,但不能正常工作——
Update Table2
Set New = New - (CASE Type WHEN 'N' THEN (Table1.qty) Else 0 End),
Old = Old - (CASE Type WHEN 'O' THEN (Table1.qty) Else 0 End),
Old = Old - (CASE Type WHEN 'O' THEN (Table1.qty) Else 0 End),
Other= Othere- (CASE Type WHEN is Null THEN (Table1.qty) Else 0 End)
from table1
inner join table2
On table1.catId = table2 .catId