SELECT StockCode, CASE pm1 WHEN 0 THEN 0 ELSE ABS((PM1 - Sale_1) / PM1)) * 100 END AS Forecast
FROM dbo.MOR_MainCustomer
WHERE pm1 IS NOT NULL AND sale_1 IS NOT NULL
SELECT StockCode ,
CASE
WHEN (Forecast_Prime >100) THEN 0
ELSE 100 - Forecast_Prime
END AS FORECAST
FROM
(
SELECT StockCode ,
CASE pm1
WHEN 0 THEN 0
ELSE ABS((PM1 - Sale_1) / PM1)) * 100
END AS Forecast_Prime
FROM dbo.MOR_MainCustomer
WHERE (pm1 IS NOT NULL) AND (sale_1 IS NOT NULL)
) AS PRIMARY_QUERY
//P.S. CASE เขียนได้สองแบบค่ะ แบบระบุค่ากับแบบกำหนดเงื่อนไขเอง