I have two columns ActivityCount and ParentValue. I created an expression:
ROUND((ActivityCount / ParentValue) / 100,16) * 100
But the problem is it returns NULL for some columns and I wanted to replace NULL with 0. I can't seem to find the answers.
Expression:
ISNULL(ParentValue) || (ParentValue == 0) ? 0 : ROUND(ISNULL(ActivityCount) ? 0 : ActivityCount / ParentValue / 100,16) * 100
For readability:
ISNULL(ParentValue) || (ParentValue == 0)
? 0
: ROUND((ISNULL(ActivityCount)
? 0
: ActivityCount / ParentValue) / 100
, 16) * 100
What it does:
If ParentValue field is NULL or has Zero, you do not have to perform a calculation because you will encounter Divide by Zero error. So, simply exit the expression by returning 0.
If ActivityCount field is NULL, replace it with zero before performing the calculation.
Recommendation:
I would recommend using COALESCE on the source query to replace the NULL values with zeroes and the calculation, if possible.
Test with ISNULL (SSIS, not the SQL expression), use the conditional operator
ISNULL(ROUND((ActivityCount / ParentValue) / 100,16) * 100) ? 0 : ROUND((ActivityCount / ParentValue) / 100,16) * 100
Try like below... it will help you...
ISNULL(ROUND((ActivityCount / ParentValue) / 100,16) * 100) || (ROUND((ActivityCount / ParentValue) / 100,16) * 100)= "" ? 0 : ROUND((ActivityCount / ParentValue) / 100,16
I would have said:
ISNULL(ActivityCount) || ISNULL(ParentValue) ? 0 : ROUND((ActivityCount/ParentValue)/100,16)*100
That way you are testing the simplest cases that will cause a NULL result rather than calculating the end case multiple times in your conditional.
(That being said, any of these will technically work.)
Related
Problem 1
SELECT
f.MANHOUR_TOTAL,
f.MATERIAL_TOTAL,
e.NILAI_RATE * f.MANHOUR_TOTAL AS LABOUR_TOTAL
Which is the existing data is e.NILAI_RATE = 4.00 and MANHOUR_TOTAL = 11.00. The result is 44.0000.
How to keep two digits behind comma on LABOUR_TOTAL as 44.00
Problem 2, I want to use those LABOUR_TOTAL to next operation like this :
SELECT
f.MANHOUR_TOTAL,
f.MATERIAL_TOTAL,
e.NILAI_RATE * f.MANHOUR_TOTAL AS LABOUR_TOTAL
LABOUR_TOTAL + f.MATERIAL_TOTAL AS FINISHING_TOTAL
But it gives me null value on FINISHING_TOTAL. Is it possible to do like this ? Any help and suggestions is so appreciated.
For Problem 1 you want ROUND:
SELECT ROUND(e.NILAI_RATE * f.MANHOUR_TOTAL, 2) AS LABOUR_TOTAL
should do it!
For Problem 2, consider this:
SELECT
f.MANHOUR_TOTAL,
f.MATERIAL_TOTAL,
ROUND(e.NILAI_RATE * f.MANHOUR_TOTAL, 2) AS LABOUR_TOTAL,
ROUND((e.NILAI_RATE * f.MANHOUR_TOTAL) + f.MATERIAL_TOTAL, 2) AS FINISHING_TOTAL
I don't understand why the result of:
4 / 3 * Math.PI * Math.pow(radio,3)
is different of:
(4 * Math.PI * Math.pow(radio,3)) / 3
I use this in a program to calculate the sphere's volume.
First, lets insert some extra brackets ... for illustrative purposes:
This code:
4 / 3 * Math.PI * Math.pow(radio,3)
is equivalent to
((4 / 3) * Math.PI) * Math.pow(radio,3)
In words ... first we divide 4 by 3, then we multiply it by Pi, then we multiply by radio to the power of 3.
Now lets look at the first sub-expression (4 / 3). Since the operands are both integers, this is an integer division, and it produces an integer answer. And that answer is 1. The answer you really need there is 1.33333... but that isn't an integer.
How to fix it? Changing either or both of the numbers to double literals will cause the division to be treated as a floating point (double) division, and that will you the best possible approximation to 4 thirds.
4.0 / 3.0 * Math.PI * Math.pow(radio,3)
First of all check for precedence of operator.
In your case precedence order is :
() > * > / (Note : this precedence is for C operators)
So for above operations result must be same for both cases.
Let's take
Case 1 :
4 / 3 * Math.PI * Math.pow(radio,3)
Here Math.PI = 3.14
Suppose Math.pow(radio , 3) = 10
In this scenario first multiplication will happen
result = 4*3.14*10 = 125.60000000000001
125.600/3 = 41.86666666666667
Case 2 :
'(4 * Math.PI * Math.pow(radio,3)) / 3'
Here first it will calculate () block then / operator
i.e (4 * Math.PI * Math.pow(radio,3))
Here again result will be same as before
I have a worksheet where I have defined names for certain cells. These cells will be used in the function and I am calling them using their names.
However, I get 0 as a return of the function when I call it with Excel, as if the names were not linked or had a 0 value.
Below is the code I wrote. "Sum_Len_1", "L_W_2" ans "L_W_1" are the names I gave to the source cells.
Function min_w(depth)
If depth < Sum_Len_1 Then
min_w = L_W_1 * 0.868 * depth / 1000
Else
min_w = L_W_1 * 0.868 * Sum_Len_1 / 1000 + L_W_2 * 0.868 * (depth - Sum_Len_1) / 1000
End If
End Function
How can I solve the problem?
If you just write min_w = L_W_1 * 0.868 * depth / 1000 vba thinks L_W_1 it's variable (of the type variant with value=0).
You have to do it like this Range("L_W_1").Value to reference the named cell.
It should work if you change it to:
Function min_w(depth As Long)
If depth < Range("SUM_LEN_1").Value Then
min_w = Range("L_W_1").Value * 0.868 * depth / 1000
Else
min_w = Range("L_W_1").Value * 0.868 * Range("SUM_LEN_1").Value / 1000 + Range("L_W_2").Value * 0.868 * (depth - Range("SUM_LEN_1").Value) / 1000
End If
End Function
You can just put them in brackets to mark them as a range:
[Sum_Len_1].Value, [L_W_2].Value and [L_W_1].Value
Can I ask for help on a SQL Statement please, I have to do the calculation inline and cannot declare variables for it
Calculation:
-91000000 / 2700000 = -33.7037037037
I need the remainder (7037037037 - but only up to 6 characters ) to be multiplied by 15000
703703 / 15000 = Final Answer of 49.913533
I thought I could do this:
select cast(ParseName(abs(cast(-91000000 as decimal)/ 2700000 ) %1,1) as numeric(8,8)) / 15000
WITH cte AS
(
SELECT -91000000 AS x, 2700000 AS y
)
SELECT ABS(ROUND((CAST(x AS decimal) / CAST(y AS decimal)) - (x/y), 6)) * 1000000 / 15000 FROM CTE
when I run the SSRS report with the following expression below, I get an error that says: wrong number of arguments. I get this error at the ).IsNaN, part of my statement. I don't see what i'm missing. Can anyone please help?
=iif(
(
(
(Sum(Fields!UNITS.Value, "SFY2011_SW_T19") / Sum(Fields!DISTINCT_CLIENT_PAY.Value, "SFY2011_SW_T19"))
- (Sum(Fields!UNITS.Value, "SFY2010_SW_T19") / Sum(Fields!DISTINCT_CLIENT_PAY.Value, "SFY2010_SW_T19"))
)
/ (Sum(Fields!UNITS.Value, "SFY2010_SW_T19") / Sum(Fields!DISTINCT_CLIENT_PAY.Value, "SFY2010_SW_T19"))
).IsNaN,
0.00,
(
(
(Sum(Fields!UNITS.Value, "SFY2011_SW_T19") / Sum(Fields!DISTINCT_CLIENT_PAY.Value, "SFY2011_SW_T19"))
- (Sum(Fields!UNITS.Value, "SFY2010_SW_T19") / Sum(Fields!DISTINCT_CLIENT_PAY.Value, "SFY2010_SW_T19"))
)
/ (Sum(Fields!UNITS.Value, "SFY2010_SW_T19") / Sum(Fields!DISTINCT_CLIENT_PAY.Value, "SFY2010_SW_T19"))
)
)
The particular error is probably because you should do IsNaN(Value) instead of Value.IsNaN but just in case you're trying to use Iif() to prevent a divide by zero error, I'll give you a bonus tip.
Doing this...
=Iif(CouldBeZero = 0, 0, SomeValue / CouldBeZero)
...will always throw an error when CouldBeZero = 0 because the division is evaluated first and the result passed to the Iif() function.
Instead do this...
=Iif(CouldBeZero = 0, 0, SomeValue / Iif(CouldBeZero = 0, 1, CouldBeZero))
...to ensure the division works and zero is displayed.
Instead of having that big complex value before .IsNAN, just use Single.IsNAN