Hi,
I am having trouble writing a query,
My query calculates discount using some fields of a table, like,
(CASE WHEN [cLineDiscTag] = '2' THEN
(CASE WHEN [cReceiptsMultiDivFlag] = 'D' THEN
(nTrnsQty * nLineDisc1) / [nReceiptsCurrConRate]
ELSE
(nTrnsQty * nLineDisc1) * [nReceiptsCurrConRate]
END)
ELSE
(CASE WHEN [cReceiptsMultiDivFlag] = 'D' THEN
(nTrnsQty * nUnitPrice * nLineDisc1 / 100) / [nReceiptsCurrConRate]
ELSE
(nTrnsQty * nUnitPrice * nLineDisc1 / 100) * [nReceiptsCurrConRate]
END)
END) As Discount1
Now in the query at the same level(it has many subqueries), i need to calculate discount2 and discount3, which needs value of discount1. I tried naming the whole table as A, then using this as subquery and so on.. but it makes things complicated.
Is their any other proper method
Please also tell me if calculation can be optimized further.
Thank You.