How to work with Null in summation - oracle

I am doing tot := tot + v_row.SAVINGS; where tot is initialized to 0 and v_row.SAVINGS is initialized to null.
Now I using nvl function to get value 0 instead of null for v_row.Savings as I need 0 in summation if there is Null but when I return v_row, if summation value shows as 0 then I need to have null or blank space value in return, how can I do it.
Note: I am using this for reporting purpose and so if value of summation is zero then I should show null or blank space in report.

NULLIF(expr1, expr2) is logically equivalent to
CASE WHEN expr1 = expr 2 THEN NULL ELSE expr1 END
NVL(expr1, expr2)
tot may be NULL and v_row.SAVINGS may be NULL.
Use NVL to ensure a number is returned for the addition
Use NULLIF to return NULL if the sum is NULL
It may be more appropriate to coerce the tot to NULL just before you return.
Example:
tot := NULLIF(NVL(tot, 0) + NVL(v_row.SAVINGS, 0), 0)

This should work:
tot := case when tot + v_row.SAVINGS = 0 then null else tot + v_row.SAVINGS end;

Related

Usage of CASE Statements in WHERE Clause - PL/SQL

Can someone tell me what is the use of last 'Y' in the " end = 'Y') "
( case
when a = 'STAGE PAYMENT' then
'Y'
when b not IN ('To be Received', 'Received') then
'N'
when c != (d - NVL(e, 0) - NVL(f, 0) - NVL(g, 0)) then
'Y'
when NVL(h, 0) + NVL(i, 0) + NVL(j, 0) <> 0 then
case
when c != k then
'Y'
when (-l != NVL(e, 0) + NVL(f, 0) + NVL(g, 0) + NVL(m, 0)) then
'Y'
else 'N'
end
else 'N'
end = 'Y')
Also, is there any way of optimizing this?
Thanks!
Last end = 'Y') is nothing but the comparision.
Your case statement is generating one value based on condition and if it is Y then that condition will be statisfied and row will be considered in the result.
Lets say, If a = 'STAGE PAYMENT' is true for some record then your case statement will generate Y as output, which will be again compared with last end = 'Y') and returns true.
Cheers!!
As your case statement is part of the where clause, that = 'Y' is there to form the predicate.
Predicates most often have the form of <some value> <comparison operator> <other value> (there are exceptions, most notably regexp_like), where the <some value> and <other value> can be columns, functions, literal values, expressions, variables, etc.
Your case expression is simply taking up the place of the <some value>, i.e.:
select ...
from ...
where <some_col> = 1
and case .... end = 'Y'

Evaluating statements with negative numbers in Pascal

the following piece of code is meant to find the maximum number from a range of entered numbers. The entry is concluded once 0 is entered (0 is not taken into account when determining the maximum). It seems to be working for positive numbers but for a range of negative numbers it always says maximum is 0. Can anyone tell me where the error is?
program MaximumEndZero (input, output);
var
Max,
Value : integer;
begin
writeln('Enter numbers');
readln(Value);
if (Value = 0) then
writeln('No input')
else
begin
Max := Value;
while Value <> 0 do
begin
readln(Value);
if Value > Max then
Max := Value
end;
writeln('Max is: ', Max)
end;
end.
Thanks vm
You have created this problem by yourself by using the value of Value to signal the end of the program and then not taking account of this special meaning (Value = 0) in your code which works out which value is the maximum.
Just change your code to
if (Value <> 0) and (Value > Max) then
Max := Value
The (Value <> 0) and effectively says "ignoring Value if it happens to be zero ..."

Swift: Why does this valid expression fail when put in ternary form?

I've written an expression that finds the largest of two variables and returns true if it is equal to or above a limit variable. They are all integers.
if max(num1, num2) >= limit {
result = 3
} else {
result = 2
}
this works fine, but if I try to put it in the more compact ternary format it is rejected with: 'could not find an overload for '>=' that accepts the supplied arguments'.
max(num1, num2) >= limit ? result = 3 : result = 2
I've tried putting the conditional in various bracket configurations but it still fails. Any ideas?
Further experimentation reveals that the problem is related to the limit var being set as implicitly unwrapped variable, although this is set in the init block:
limit: Int!
init (num: Int) {
limit = num
}
Many thanks.
Kw
The problem here is precedence, it should "work" if you have written
max(num1, num2) >= limit ? (result = 3) : (result = 2)
// ^ ^
Note the precedence of each operator:
?:: associativity right precedence 100 (higherThan: AssignmentPrecedence)
=: associativity right precedence 90
so a ? b : c = d will be evaluated as (a ? b : c) = d since the ternary conditional operator has higher precedence than assignment.
Here in max(num1, num2) >= limit ? result = 3 : result, the type of result = 3 is () and result is Int, so there is a type mismatch between the two arms and caused error.
(However I'm not sure why the message talks about >=, please check if your num1 and num2 are also Int.)
That said, it is very unconventional (i.e. bad style) to put an assignment inside a ?:, typically you want to write this instead:
result = max(num1, num2) >= limit ? 3 : 2

cognos report studio multiple columns average

Im relatively new to cognos so please bear this in mind.
I wanted to create a report in report studio where i have 3 measures ( A ,B , C ) as columns and a 3rd calculated column as the average of these columns.
However , when using the average function , i cannot add multiple inputs .
i tried the arithmetic alternative ( a+b+c)/3 because this will not handle cases when values are null
Thank you in advance
If nulls are not zero for average
case when
not (a is null and b is null and c is null)
then
(coalesce(a,0) + coalesce(b,0) + coalesce(c,0)) /
(case when a is not null then 1 else 0 end +
case when b is not null then 1 else 0 end +
case when c is not null then 1 else 0 end)
end
I would create a value count expression to count the non-null columns:
Value Count
CASE WHEN [A] is not null THEN 1 ELSE 0
+
CASE WHEN [B] is not null THEN 1 ELSE 0
+
CASE WHEN [C] is not null THEN 1 ELSE 0
Then you can use this new data item as the dividend for your average calculation:
Average
(coalesce([A],0) + coalesce([B],0) + coalesce([C],0))/[Value Count]
If division by 0 is a problem, you can wrap the average expression in another CASE to return null when [Value Count] is 0:
CASE
WHEN [Value Count] > 0 THEN (coalesce([A],0) + coalesce([B],0) + coalesce([C],0))/[Value Count]
ELSE null
END

Multiple if statement in VB for ArcGIS

I have a shape-file with two columns (Thirri_2a and Thirri 2b) both are filled with positiv and negativ double values. No i want to summarize both in a new column named "Thirri". Therefore i used a if statement to check true values (not equal to 0).
Dim x As Double
If (([Thirri_2a] = 0) And ([Thirri_2b] = 0)) Then
x = 0
ElseIf (([Thirri_2a] = 0) And ([Thirri_2b] <> 0)) Then
x = [Thirri_2b]
ElseIf (([Thirri_2a] <> 0) And ([Thirri_2b] = 0)) Then
x = [Thirri_2a]
Else (([Thirri_2a] <> 0) And ([Thirri_2b] <> 0))
x = (( [Thirri_2a] + [Thirri_2b] )/2)
End If
ArcGIS gives me the wonderful Errormessage:
"Error 999999"
There should be no problem with that code other than the type specifier as #Ekkehard.Horner notes, but that entire block should go in the Pre-logic Script Code block.
In the following field, which should be labelled Thirri =, just put x.

Resources