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