bitwise operation in sql - 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, ...

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.

Redshift - Breaking number into 10 parts and finding which part does a number fall into

I am trying to break down a given number into 10 equal parts and then compare a row of numbers and see in which of the 10 parts they fall under.
ref_number, number_to_check
70, 34
70, 44
70, 14
70, 24
In the above data set, I would like to break 70 into 10 equal parts (in this case it would be 7,14,21, and so on till 70). Next I would like to see in which "part" does the value in column "number_to_check" fall into.
Output expected:
ref_number, number_to_check, part
70, 34, 5
70, 44, 7
70, 14, 2
70, 24, 4
You want arithmetic. If I understand correctly:
select ceiling(number_to_check * 10.0 / ref_number)
Here is a db<>fiddle (the fiddle happens to use Postgres).

Explain the difference in this while loop please

Example:
x = 0
while x <= 10:
x += 2
print(x)
Results for this will be 0, 2, 4, 6, 8 , 10
If I switch the postion of print(x) and x += 2. The results will be 2, 4, 6, 8, 10, 12.
Please explain to me the thought process for this.
Thanks
I suppose this is Python. Basically the code will get executed in the order it is written. I suggest you to take a sheet of paper and follow the lines of code, trying to figure out what happens and what is the value of your variable at each step. The output will be the values that your variable had when it got printed with the print function.

Hadoop Pig Query

Whenever I try to run this query, it returns zero records even though I know there are records that meet this criteria.
I want to find records where column 10 is equal to 0, 1, 2, or 3 and column 16 is equal to 4, 5, 6, or 7.
Pig Script:
newdata = FILTER data BY ((((($10==0) OR ($10==1)
OR ($10==2)
OR ($10==3)))) AND
(((($16==4) OR ($16==5)
OR ($16==6)
OR ($16==7)))));
Column indexes start from 0, so the 10th column will be $9 and 16th column with me $15.Try this
newdata = FILTER data BY ((($9==0) OR
($9==1) OR
($9==2) OR
($9==3))
AND
(($15==4) OR
($15==5) OR
($15==6) OR
($15==7)));

Search array with logical condition

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.