Search code examples
sqlsql-serversql-server-2000

Count average cost and update cost of all products in column with conditions


please help me with following:

I have table "Products" and have following task: Count average cost and update cost of all products in column "UnitPrice" with conditions if current price > average then -10% if < +20%

By the way need to use variables

Please help me to write script I use SQL Server 2000

I wrote somthing like that:

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%

Thanks everybody before.


Solution

  • 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)