Case statement in where clause with In clause - case

I tried using case statement in the where clause, which needs to filter my where condition based on the parameter value passed to my procedure, but it didn't worked
where case when p_parameter ='Y'
then 'Y'
else ('Y','N')
END in Hidden_flag_column

Try
where
case when p_parameter ='Y'
then case when Hidden_flag_column = 'Y' then 1 else 0 end
else case when Hidden_flag_column in ('Y','N') then 1 else 0 end
END = 1
Example with p_parameter = 'Y' as well as 'N' and Hidden_flag_column = 'N'.
select * from dual
where
case when 'N' ='Y'
then case when 'N' = 'Y' then 1 else 0 end
else case when 'N' in ('Y','N') then 1 else 0 end
END = 1
gives 1 row
while
select * from dual
where
case when 'Y' ='Y'
then case when 'N' = 'Y' then 1 else 0 end
else case when 'N' in ('Y','N') then 1 else 0 end
END = 1
gives 0 rows.

Related

Netsuite - Saved Search Formula(text) Case statement multiply 2 fields

I'm trying to get the product of 2 fields in the 'ELSE' portion of a CASE statement of a Formula(Text) row in a Netsuite saved search, but I keep getting 'ERROR: Invalid Expression' when I run the search. Current formula is:
CASE WHEN {binonhandcount} = 0 THEN '0' ELSE NVL({binonhandcount}, 0) * NVL({locationaveragecost}, 0) END
I've tried to simplify it by doing something like this:
CASE WHEN {binonhandcount} = 0 THEN '0' ELSE 1 + 1 END
But it still fails with an invalid expression error. All of the Googling I've done leads me to believe that this should work, but I can't seem to find my mistake. I'm hoping the extra eyes here can give me a kick in the right direction. Thank you.
The data type returned from the formula needs to match the Formula type selected. You can correct your formula by setting it to a Formula (Numeric) type and simply removing the quotes around the '0' after the first THEN:
CASE WHEN {binonhandcount} = 0 THEN 0 ELSE NVL({binonhandcount}, 0) * NVL({locationaveragecost}, 0) END
Or if you really want a text formula for some reason, you can wrap the ELSE statement in a TO_CHAR function:
CASE WHEN {binonhandcount} = 0 THEN '0' ELSE TO_CHAR(NVL({binonhandcount}, 0) * NVL({locationaveragecost}, 0)) END
In your NetSuite saved search replace Formula(text) to Formula(Numeric).
And your formula would be:
CASE WHEN {binonhandcount} = 0 THEN 0 ELSE NVL({binonhandcount}, 0) * NVL({locationaveragecost}, 0) END
Please remove the string from THEN '0'. You should be fine.

Can you explain to me how this code operates under the hood

def aa(a):
if a == 1:
return 1
else:
return aa(a-1) + 1
I am able to understand the above recursion and how it is working.
However I cannot understand how this recursion is working. Can you help me?
def aa(a):
if a == 1:
return 1
else:
return aa(a-1) + aa(a-1)
I tried debugging as well. The cursor was jumping places. Didnt help me figure it out .
Think of this on a simple level, say with a number like 2:
def aa(a):
if a == 1:
return 1
else:
return aa(a-1) + aa(a-1)
Since 2 is greater than 1, we go straight to the else.
The else asks for us to evaluate aa(a-1) and then add aa(a-1) to it. To do this, we must first evaluate the leftmost value of this expression;
With a starting value of 2 it's easy to see that aa(a-1) is aa(2-1) or just aa(1) which will always return a value of 1 due to the first if statement. So the else will evaluate to aa(1) + aa(1) or simply 1 + 1
So what happens when we pass an initial value of 3? The logic follows like this (bold are the functions that will be evaluated on the next line):
3 == 1 ? no
aa(3-1) + aa(3-1) // aa(2) + aa(2)
2 == 1 ? no
aa(2-1) + aa(2-1)
1 == 1 ? YEP, return 1
1 + aa(2-1)
1 == 1 ? YEP, return 1
1 + 1
2 + aa(3-1)
2 == 1 ? no
aa(2-1) + aa(2-1)
1 == 1 ? YEP, return 1
1 + aa(2-1)
1 == 1 ? YEP, return 1
1 + 1
2 + 2
Essentially you're evaluating the left side of the equation, and within that left side, you're evaluating a left and a right. But you evaluate the left first, then the left first etc. etc. until you finally get a result and then evaluate the right, continuing until you have an answer:
left right
left right
value
value right
value
value right
left right
value
left right
value
value value

Autoit Why is (0 <> "Test") False

I am fetching a numeric value from an HTML table. If it fails to fetch the value I fill the value "NA" instead. Here is the odd part 0 <> "NA" is false, 0 = "NA" is true, 0 == "NA" is False. I get that = is not case sensitive, and == is, but I thought <> was case sensitive... So why does it work like this?
Local $x = 0
If $x <> "Test" Then
MsgBox(0,"","x <> Test")
Else
MsgBox(0,"","x = Test")
EndIf
With this exaplle I get a message box "x = Test"
but I thought <> was case sensitive
According to the docs, it is not a string-specific comparison operator like ==. Rather, it’s just the negation of =, so your string will still be interpreted as an integer – both "NA" and "Test" becoming 0 – and fail to satisfy 0 <> 0.
Tests if two values are not equal. Case insensitive when used with strings. To do a case sensitive not equal comparison use Not ("string1" == "string2")

Netsuite CASE WHEN expression in custom search is returning “invalid expression”

Can someone help me with what is wrong with this expression
case When ({item.locationquantitybackordered} > 0 AND {quantitycommitted} > 0)
Then ({item.locationquantitybackordered}+{quantitycommitted})
Else
when {item.locationquantitybackordered} > 0
Then {quantitycommitted} = 0
Else {item.locationquantitybackordered} = 0
{item.locationquantitybackordered}+{quantitycommitted}
End
End
It looks like there are a few things I would fix on this.
First, you don't need ELSE before WHEN. There should only be one WHEN, at the end of the CASE statement as the last condition.
Second, you have two END statements, which isn't necessary - only one is needed per CASE statement. You might need one if you had a nested CASE statement, but you don't so it is unnecessary.
Finally, your ELSE actually doesn't make much sense - you have two statements in there, {item.locationquantitybackordered} = 0 (which is a test, not a value) and {item.locationquantitybackordered} + {quantitycommitted}, which is a proper value, and it is the same as your first case above.
I'd rewrite your statement as:
CASE
WHEN ({item.locationquantitybackordered} > 0 AND {quantitycommitted} > 0) THEN ({item.locationquantitybackordered} + {quantitycommitted})
WHEN {item.locationquantitybackordered} > 0 THEN 0
ELSE {item.locationquantitybackordered} + {quantitycommitted}
END

Stuck on an Ada Program - Stuck on Input

I have a pretty simple Ada project on my hands. The task is to take a collection of a "voter's" votes and compare it to each "candidate's" score and determine which candidate best matches the voter.
The input looks like this, followed by the output that should be put out:
Input:
0 0 0 1 1 1 -1 -1 -1 1
7
A
1 1 1 1 1 1 1 1 1 1
B
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
C
1 -1 1 -1 1 -1 1 -1 1 -1
D
1 0 1 0 1 0 1 0 1 0
E
0 -1 0 -1 0 -1 0 -1 0 -1
F
1 1 1 1 0 0 0 0 0 0
G
0 0 0 1 -1 0 0 -1 1 1
Output:
A
F
G
So far, I have a procedure that will take the votes of each candidate and compare them to the votes of the voter. I know what I need to do as I have done it before in Java, but I am not sure how I should take the input in Ada. Here is what I have so far.
with Ada.Text_IO, Ada.Integer_Text_IO;
use Ada.Text_IO, Ada.Integer_Text_IO;
procedure Candidates is
-- take an array of candidates and determine which candidate best matches
-- the user's votes, then return those candidates
Number_Of_Candidates: Integer;
subtype VoterArray_Index is Integer range 1..10;
subtype CandidatesArray_Index is Integer range 1..Number_Of_Candidates;
type VoterArray is array(VoterArray_Index) of Integer;
type CandidatesArray is array(Character range 'A' .. 'Z') of array;
type Best_CandidatesArray is array(CandidatesArray_Index) of array;
Voter: VoterArray;
Candidates: CandidatesArray;
Best_Candidates: Best_CandidatesArray;
function Get_Input() is
-- get the input and put it into the correct arrays
Current_Line : string;
begin
Get(Current_Line);
function Get_Best_Score(CandidatesArray: in out CandidatesArray) is
-- go through the arrays and find the best score
SameAnswers: Integer;
DifferentAnswers: Integer;
BestScore: Integer;
subtype CandidateArray is array(VoterArray_Index) of Integer;
Candidate: CandidateArray;
begin
for I in CandidatesArray_Index loop
Candidate := Candidates(I);
for J in VoterArray_Index loop
if Candidate(J) /= 0 and Voter(J) /= 0 then
if Candidate(J) /= Voter(J) then
DifferentAnswers := DifferentAnswers + 1
else
SameAnswers := SameAnswers + 1
end if;
end if;
end loop;
if SameAnswers - DifferentAnswers >= BestScore then
Best_Candidates(I) := Candidate;
end if;
SameAnswers := 0;
DifferentAnswers := 0;
end loop;
end Get_Best_Score;
As you can see, I'm not sure how to take the numbers and put them into an array. If you have any suggestions or a different way I should go about things, I'm all ears.
Thanks in advance.
You could use streams in order to read the data in:
Integer'Read( STREAM_HANDLE, VARIABLE )
Another option is reading the values via Get for each element of your array, I'd recommend a helper-function in case you need to tweak the procedure for handling format changes:
Function Get_Vote ( File : File_Type ) Return Integer is
begin
Return Result : Integer do
Integer_IO.Get(
File => File,
Item => Result
);
End return;
end Read_Votes;
and
For Index in VOTE_ARRAY'range loop
VOTE_ARRAY( Index ) := Get_Vote( INPUT_FILE );
End loop;
Because the number of rows is given in the file, a constrained array has to be large enough to hold all possible elements. Instead, you can declare an unconstrained array:
subtype Voter_Index is Positive range 1 .. 10;
type Voter_Array is array(Voter_Index) of Integer;
type Candidate_Array is array(Character range <>) of Voter_Array;
Later, when you know the actual count, you can allocate only the space actually required for the array. This declaration puts Candidates on the stack in a nested scope:
Number_Of_Candidates := ...;
declare
Candidates : Candidate_Array(
'A' .. Character'Val(Character'Pos('A') + Number_Of_Candidates));
begin
...
end;
Alternatively, you can allocate space on the heap:
type Candidate_Array_Ptr is access Candidate_Array;
Candidates: Candidate_Array_Ptr;
begin
Number_Of_Candidates := ...;
Candidates := new Candidate_Array(
'A' .. Character'Val(Character'Pos('A') + Number_Of_Candidates));
end;
In either case, you can access the array elements as required:
for i in Candidates'Range loop
for j in Voter_Array'Range loop
Ada.Integer_Text_IO.put(Candidates(i)(j), 5);
end loop;
Ada.Text_IO.New_Line;
end loop;
Addendum: This approach assumes that candidate names are consecutive Characters. As an alternative, consider an array of Candidate_Record, where each Name is read from the file:
type Candidate_Record is
record
Name : Character;
Votes : Voter_Array;
end record;
type Candidate_Array is array(Positive range <>) of Candidate_Record;
Candidates : Candidate_Array(1 .. Number_Of_Candidates);
for i in Candidates'Range loop
Ada.Text_IO.Put(Candidates(i).Name & ':');
for j in Voter_Array'Range loop
Ada.Integer_Text_IO.Put(Candidates(i).Votes(j), 5);
end loop;
Ada.Text_IO.New_Line;
end loop;

Resources