Hi
I have an SQL statement:
SQL = “SELECT DISTINCT
`Part No`, Description, `Invoice No`, `Planning Element`,
`Invoice Line`, `Invoice Date`, `Invoice Creator`,
`QTY Invoiced`, `Value (GBP)`, `Cost Of Sale`, `Currency Code`,
`Customer Group`, Region, `Sold to`, `Delivery Note`,
`Business Unit`, `Product Manager`, `Product Group`,
`Product Manager Code`, `Order Creator Name`,
`Customer Purchase Order`, Customer, `Country Code`,
`Sales Region`
FROM tblSales
WHERE (`Business Unit` = 'BA')
ORDER BY `Part No` ASC”
This returns some sales data.
I trigger this statement using within my VB code:
rsExecute.Open SQL, cn, adOpenStatic, adLockOptimistic, adCmdText
This works fine but I’ve been asked to provide an extra calculated margin field. The problem is that I need to put a condition on the statement as some of the output will result in a division by zero which causes an error in SQL execution. The resulting recordset is then set as the data in a datagrid.
I need to be able to say…
If `Value (GBP)` = 0 AND `Cost Of Sale` > 0 THEN
Margin = -100
ELSE IF `Value (GBP)` = 0 AND `Cost Of Sale` < 0 THEN
Margin = 0
ELSE
Margin = ((`Value (GBP)` - `Cost Of Sale`) / `Value (GBP)`) * 100
END IF
This would resolve any division by zero issues but how (if possible?) could I put this sort of condition within my SQL statement?
Thanks
I have an SQL statement:
SQL = “SELECT DISTINCT
`Part No`, Description, `Invoice No`, `Planning Element`,
`Invoice Line`, `Invoice Date`, `Invoice Creator`,
`QTY Invoiced`, `Value (GBP)`, `Cost Of Sale`, `Currency Code`,
`Customer Group`, Region, `Sold to`, `Delivery Note`,
`Business Unit`, `Product Manager`, `Product Group`,
`Product Manager Code`, `Order Creator Name`,
`Customer Purchase Order`, Customer, `Country Code`,
`Sales Region`
FROM tblSales
WHERE (`Business Unit` = 'BA')
ORDER BY `Part No` ASC”
This returns some sales data.
I trigger this statement using within my VB code:
rsExecute.Open SQL, cn, adOpenStatic, adLockOptimistic, adCmdText
This works fine but I’ve been asked to provide an extra calculated margin field. The problem is that I need to put a condition on the statement as some of the output will result in a division by zero which causes an error in SQL execution. The resulting recordset is then set as the data in a datagrid.
I need to be able to say…
If `Value (GBP)` = 0 AND `Cost Of Sale` > 0 THEN
Margin = -100
ELSE IF `Value (GBP)` = 0 AND `Cost Of Sale` < 0 THEN
Margin = 0
ELSE
Margin = ((`Value (GBP)` - `Cost Of Sale`) / `Value (GBP)`) * 100
END IF
This would resolve any division by zero issues but how (if possible?) could I put this sort of condition within my SQL statement?
Thanks