要在select SQL查询中填充临时总值字段,但是使用两个值的数学创建此临时字段。如果满足某个条件,则应该应用某个数学,否则将应用另一个数学,等等。
示例:
SELECT Price, Quantity, Type,
CASE WHEN Type = 'Fixed' THEN ((Price * Quantity)/100)
WHEN Type = 'Option' THEN ((Price * Quantity)*100)
ELSE (Price * Quantity)
END AS Value
FROM Holding..Position
答案 0 :(得分:2)
你差不多......
SELECT Price
, Quantity
, [Type]
, CASE WHEN [Type] = 'Fixed' THEN ( (Price * Quantity) * 1.00 /100)
WHEN [Type] = 'Option' THEN ( (Price * Quantity) * 1.00 * 100)
ELSE (Price * Quantity) * 1.00
END AS Value
FROM Holding..Position
按1.00
计算时间,以确保在答案中获得小数位数。
答案 1 :(得分:0)
您的查询看起来很好。如果要将列添加到表中,可以使用计算列:
alter table holding..Position
add Value as (CASE WHEN [Type] = 'Fixed' THEN (Price * Quantity)/100
WHEN [Type] = 'Option' THEN (Price * Quantity)*100
ELSE (Price * Quantity)
END);