计算具有条件的列中所有产品的平均成本和更新成本

时间:2013-03-25 10:18:37

标签: sql sql-server sql-server-2000

请帮助我:

我有“产品”表并具有以下任务:计算“UnitPrice”列中所有产品的平均成本和更新成本,条件是当前价格>平均然后-10%如果< 20%

顺便说一下需要使用变量

请帮我写脚本我用SQL Server 2000

我写了这样的东西:

DECLARE @Average_cost

SELECT @Average_cost = AVG(UnitPrice)  FROM Products  

UPDATE Products
SET UnitPrice = CASE WHEN UnitPrice > @Average_cost 
                      THEN -10% 
                      ELSE UnitPrice < @Average_cost 
                      THEN + 20%

先谢谢大家。

2 个答案:

答案 0 :(得分:0)

试试这个

UPDATE Products
SET UnitPrice = (CASE WHEN UnitPrice > @Average_cost 
                      THEN UnitPrice - UnitPrice/10 
                      WHEN UnitPrice < @Average_cost 
                      THEN UnitPrice + UnitPrice/5                      
                END)     

答案 1 :(得分:0)

DECLARE @Average_cost

SELECT @Average_cost = AVG(UnitPrice)  FROM Products  
UPDATE Products
SET UnitPrice = (CASE WHEN UnitPrice > @Average_cost 
                      THEN UnitPrice - (UnitPrice * .1)
                      WHEN UnitPrice < @Average_cost 
                      THEN UnitPrice + (UnitPrice * .2)  
                      ELSE @Average_cost                   
                END)