我在SQL Server中有一个类似于下面的表:
ID Value
A 5
B 1
C 2
D 3
我需要插入一个ID为' E'的新行。其值为A.Value-D.Value,即(5-3 = 2) 输出
ID Value
A 5
B 1
C 2
D 3
E 2
答案 0 :(得分:1)
使用max
和insert
行获取值。这假设A
和C
表格中只能有一个值。
insert into tablename (id,value)
select 'E',
max(case when id='A' then value end) - max(case when id='C' then value end)
from tablename