Search array with logical condition - sql

I have integer array for which I have to check whether items meet the logical condition.
For example:
array: [1, 4, 17, 22, 45]
condition: 1 AND 17 AND (22 OR 49)
How to check that elements of array fulfill the condition.
I have elements of array stored in DB as rows with common identifier:
ID_1 | 1
ID_1 | 4
ID_1 | 17
ID_1 | 22
ID_1 | 45
If someone has any idea how to check that those values meet condition through SQL or search across array I would be grateful.

Maybe something like this:
Module StartupModule
Sub Main()
Dim nums() As Integer = {1, 4, 17, 22, 45}
Dim hasNumbers As Func(Of Integer(), Boolean) = Function(arr)
Return arr.Contains(1) AndAlso arr.Contains(17) AndAlso (arr.Contains(22) OrElse arr.Contains(49))
End Function
Dim result As Boolean = hasNumbers(nums)
Console.WriteLine(result)
Console.ReadLine()
End Sub
End Module
EDIT
It won't do as just now read that condition would be anything. So I would use expression trees.

I assume that you want to find invoices containing item id 1 AND 17 AND (22 OR 49). Then you can do
SELECT invoice_id
FROM items
WHERE item_id IN (1, 17, 22)
GROUP BY invoice_id
HAVING COUNT(*) = 3
UNION
SELECT invoice_id
FROM items
WHERE item_id IN (1, 17, 49)
GROUP BY invoice_id
HAVING COUNT(*) = 3
This works only if the item_ids are unique per invoice.

I found a solution in the reverse logic. For each element in a condition that represents an item (not braces or logical operand, so integer) I checked whether it is element of array. If it is then it's replaced with true, otherwise with fasle.
So I get something like this:
array: [1, 4, 17, 22, 45]
original conditions
condition A: 1 AND 17 AND (22 OR 49)
condition B: 17 AND 45
will turn into
condition A: true AND true AND (true OR false)
condition B: true AND true
Then I just need to make function that will evaluate string with boolean expression into true or false.
Here is example how to evaluate logical expression.
So I didnt test array with condition then check if items in condition exist or not in array and replace them with true/false. With that I got a logical expression that should be evaluated.

Related

How can I use IF and ELSE IF in looping and display 2 statements in GAMS?

I am a beginner level in this program. I try to improve this loop according to this condition. The details are as follows:
When CUTI(k) = CUTI(k)-4 then,
1)If the result shows this CUTI(k) value greater than 0, then print this CUTI(k) value.
2)If the result shows CUTI(k) value less than 0, then print this CUTI(k) value is added 12 with showing a word "*" after the number in display, e.g. 10*, 9*
I am not sure this loop is correct and enough to add this condition. Look forward to seeing your recoomendation. :)
set k /1*20/;
parameter
CUTI(k)/1 6, 2 2, 3 8, 4 5, 5 1, 6 3, 7 7, 8 8, 9 6, 10 8,11 1, 12 2, 13 4, 14 7,
15 5, 16 2, 17 8, 18 9, 19 2, 20 10/;
loop(k,
if(CUTI(k)-4 > 0,
CUTI(k) = CUTI(k)-4;
else
CUTI(k) = (CUTI(k)-4)+12 ;
)
);
display CUTI;
Your logic looks correct. However, instead of the loop/if/else you could simplify this to one assignment:
CUTI(k) = CUTI(k)-4+12$(CUTI(k)<=4);
However, modifying the display statement by adding a * to some elements is not possible. If you need to distinguish the cases in such a statement, you might assign the values to two different parameters and display them individually.

How to access an element in array vector using metaprogramming?

Here is a table t. The data form of column arr1 is array vector.
arr1=array(DOUBLE[], 0, 10).append!([2 3 4, 4 5 7, 7 9 10])
t = table(1..3 as id, arr1, rand(100, 3) as value)
I can use a SQL statement to query for the first element in column arr1, i.e., arr1[0].
select arr1[0] from t
Output:
arr1_at
2
4
7
Now I want to query using metaprogramming.
sql(select = sqlCol('arr1[0]') ,from =t).eval()
But an error was raised as follows:
Server response: 'Unrecognized column name arr1[0]
Try the following two lines:
sql(select=sqlColAlias(<arr1[0]>,"arr1_0"), from=t).eval()
sql(select=sqlColAlias(makeCall(at, sqlCol("arr1"), 0), "arr1_0"), from=t).eval()
Output:
arr1_0
2
4
7
The first line uses the metacode <arr1[0]>.
The second line uses function makeCall to call the at function to get the value at the 0-th position in column arr1 and thus obtain the new column arr1_0.

bitwise operation in sql

please can someone help me to understand this condition val1 & 11 = 0
SELECT val FROM table WHERE val1 & 11 = 0
available for more information.
Thanks a lot
11 is 1011 in base 2. The code selects rows where 1st, 2nd and 4th bits are 0. I.e. it will match where val1 in 0, 4, 16, 20, 32, 36, 48, ...

Lookupset with duplicate records SUM not working SSRS 2008

I have the below data in a dataset called Questions and all IDs in another dataset called Dataset1
ID Answer
1 Yes
2 Yes
2 No
2 Yes
3 No
My expected output should be as below
ID Yes No
1 1 0
2 2 1
3 0 1
I am trying to match the ids from Dataset1 and get the Answer from Questions dataset.
If I just use Lookup, it is just checking the first match and ignoring the second record. For eg, in the above data, for ID-2, it is checking the first record with id 2 and counting 'Yes' and ignoring the other 'No' and 'Yes'
=Sum(iif(Lookup(Fields!ID.Value, Fields!ID.Value, Fields!answer.Value, "Questions") = "Yes", 1, 0))
I want to count all Yes and No like shown in the expected output above
I have tried using Lookupset but I couldn't get it working. Is there any easier way without using custom code. If custom code is necessary, could you please advise on how to achieve this.
Thank you in advance.
Why do you need a second dataset?
Have a matrix on the questions dataset, row grouped by ID then use a sum in each column as follows:
=SUM(IIF(Fields!answer.Value="Yes",1,0))
=SUM(IIF(Fields!answer.Value="No",1,0))
You can count the output from a lookupset by using the following custom code:
Function CountAnswers(ByVal AnswerArray As Object(), Answer As string) As Object
If AnswerArray Is Nothing Then Return cint(0)
If Answer Is Nothing Then Return "Nothing"
Dim TotalCount As Decimal = New Decimal()
Dim AnswerPosition As Decimal = New Decimal()
TotalCount = 0
For AnswerPosition = 0 to AnswerArray.GetUpperBound(0)
TotalCount = Switch(IsNothing(AnswerArray(AnswerPosition)),TotalCount, cstr(AnswerArray(AnswerPosition))=Answer,TotalCount + 1, TRUE,TotalCount)
Next
return TotalCount
End Function
Then enter the following under "Yes" and "No" on a matrix on Dataset1:
Code.CountAnswers(Lookupset(Fields!ID.Value, Fields!ID.Value, Fields!answer.Value,"Questions"),"Yes")
Code.CountAnswers(Lookupset(Fields!ID.Value, Fields!ID.Value, Fields!answer.Value,"Questions"),"No")

IF OR Statements which always evaluate to TRUE

Recently I found an error which was due to the way I constructed an IF OR statement.
The original statement:
If a = 1 OR 2 OR 3 Then
`Execute code 1`
`Else if a = 4 OR 5 OR 6 Then`
`Execute code 2`
`Else`
`Do nothing`
The corrected code:
If a = 1 OR a = 2 OR a = 3 Then
`Execute code 1`
`Else if a = 4 OR a = 5 OR a = 6 Then`
`Execute code 2`
`Else`
`Do nothing`
So, the issue was the first part of the IF statement would always evaluate true regardless of the value of a, because it also evaluates the boolean value of the number 2, which is true. So simply spelling out that each number should be compared to the value of a fixes that issue.
My questions are:
1. When using an if statement and you have a comparative operation ( if a =1) followed by "OR 2", in what situation would you actually want to look at the boolean value of the number versus comparing the number against the value of the previously referenced variable?
Is there a way to identify if statements present in the code which will logically ALWAYS be true (and probably have a error)?
Edit: the_lotus pointed out that the boolean value of each number was not being evaluated but bitwise operations were performed between all the values and then a boolean evaluation was performed on the result. In this particular case it may be possible for it to evaluate either true or false depending on the value of a, though I am still interested in identifying if statements which always evaluate true.
As per your Edit, since you want to check if the value of A falls within a range, what you could do is store the ranges as an array and perform IndexOf to see if the value of A is in the collection.
Here is a quick example:
Dim a As Integer = 4
Dim collection1() As Integer = {1, 2, 3}
Dim collection2() As Integer = {4, 5, 6}
If Array.IndexOf(collection1, a) > -1 Then
Console.WriteLine("a = 1, 2, or 3")
ElseIf Array.IndexOf(collection2, a) > -1 Then
Console.WriteLine("a = 4, 5, or 6")
Else
Console.WriteLine("a is not a value between 1-6")
End If
Fiddle: Live Demo