1

I have written a query to Select sales values from a database and apply discounts if there were any used. The discounts are either a percentage or just a flat-value. My Query applies the discounts (first case statement) and then pulls the amount that was applied.

It got me thinking if it was possible to get this done in one case statement.

Query:

SELECT
SUM(CASE 
 WHEN td.Transaction_ID IS NULL
  THEN p.Sale
 WHEN td.Transaction_ID IS NOT NULL AND d.Discount_Type = '0'
  THEN p.Sale - (p.Sale * (dp.Discount_Percent/100))
 ELSE
  p.Sale - dv.Discount_Value
END) PriceDiscounted,
SUM(CASE
 WHEN td.Transaction_ID IS NOT NULL AND d.Discount_Type = '0'
  THEN (p.Sale * (dp.Discount_Percent/100))
 WHEN td.Transaction_ID IS NOT NULL AND d.Discount_Type = '1'
  THEN dv.Discount_Value
 ELSE '0'
END) Discount
4

1 回答 1

1

SELECT 中的 CASE 语句不是函数,只能返回一个值。您可以在存储过程中实现这一点,您可以在其中使用 CASE 或 IF THEN。

于 2010-01-26T22:10:31.317 回答