I am using SQL Server 2014
and I have the following syntax in my query to compute the result of a division.
USE MyDatabase
SELECT
...
CAST(cte1.[F&B] AS FLOAT) * CAST (cte1.[RN] / cte1.[Total RN] AS FLOAT) AS [F&B_Split],
CAST(cte1.[F&B] AS FLOAT) * CAST (cte1.[RN] / cte1.[Total RN] AS FLOAT) / CAST(cte1.[GN] AS FLOAT) AS [F&B_Split_PerGN]
...
FROM
table1
Columns RN
and GN
in table1
are integers and Column F&B
is of numeric type.
Here is an extract of my results:
RN TOTALRN GN F&B F&B_Split F&B_Split_PerGN
-------------------------------------------------------------
9 9 18 1200 1200 66.6666666666667
9 10 18 3900 0 0
The calculations appear to work correctly when both RN
and TotalRN
values are the same (row 1 above) but gives 0 for F&B_Split and F&B_Split_PerGN (row 2 above).
What am I doing wrong here?