I have a table:
id name
1 A
3 B
2 C
4 D
I am trying to select a few rows using the following query in a stored procedure:
select * from test where id = all ('{1,2}');
I am expecting it to return rows with ids 1 and 2, however, it returns empty.
The following query works as expected:
select * from test where id <> all ('{3,4}');
It is returning rows with id 1 and 2. I am unable to understand why '=' operator is not working as expected but '<>' is working. I am new to this syntax. Please help how to get the expected result that is using equal operator (equivalent to IN).
I am expecting it to return rows with ids 1 and 2
You want any, not all:
where id = any ('{1,2}');
This brings id that are either equal to 1 or 2. In other words that's equivalent to id in (1, 2).
As regard to this expression:
where id <> all ('{3,4}');
This is equivalent to:
where not (id = any ('{3,4}'));
So this filters out ids 3 and 4.
Concerning your original expression:
where id = all ('{1,2}');
This does not make sense; a single value cannot be matched against all values of an array at once - so this filters out all rows.
Related
I have a query I am using IN operator and I want all the rows from in given list as shown in the picture that I want 3 rows for id 1 and one for id 2, but I only get one row for Id = 1 is there any other solution for this.
IN can't do what you want. JOIN instead:
select * from logs
JOIN (values (1),(2),(1),(1)) x (id)
ON logs.id = x.id
SELECT * FROM hr.NullValueCheck
ID Name
1 abc
2 abc
3 bcd
4 cde
https://oracle-base.com/articles/misc/all-any-some-comparison-conditions-in-sql
Query 1 :
SELECT *
FROM hr.NullValueCheck
where id > All (SELECT NULL FROM DUAL )
Nothing is coming.
But for below quesry. All records are coming while subquesry is returning is NULL same as like above query (SELECT NULL FROM DUAL )
Query 2:
SELECT *
FROM hr.NullValueCheck
where id > All (SELECT id from hr.NullValueCheck where id = 5)
Please explain me why Query 1 is returning No Records but Query 2 is returning all records.
As per my knowledge Query 1 should also return all records.
NULL is different from an empty set.
The first example is saying: "select all rows where the id is greater than all values of NULL". Or more simply, "where id is greater than 'NULL'`.
In SQL, 'NULL' generally has the semantics of "not known". If you don't know the value, then you don't know if a given id is larger. Hence, no rows are returned.
In the second example, instead has an empty set for comparison. An empty set is not NULL. Obviously, any number is greater than all numbers in an empty set. Hence, all rows are returned.
We're dealing with geographic data with our Oracle database.
There's a function called ST_Insertects(x,y) which returns true if record x intersects y.
What we're trying to do is, compare each record of table A with all records of table B, and check two conditions
condition 1 : A.TIMEZONE = 1 (Timezone field is not unique)
condition 2 : B.TIMEZONE = 1
condition 3 : ST_Intersects(A.SHAPE, B.SHAPE) (Shape field is where the geographical information is stored)
The result we're looking for is records ONLY from the table A that satisfy all 3 conditions above
We tried this in a single select statement but it doesn't seem to make much sense logically
pseudo-code that demonstrates a cross-join:
select A.*
from
tbl1 A, tbl2 B
where
A.TIMEZONE = 1 and
B.TIMEZONE = 1 and
ST_Intersects(A.SHAPE, B.SHAPE)
if you get multiples, you can put a distinct and only select A.XXX columns
With a cross-join rows are matched like this
a.row1 - b.row1
a.row1 - b.row2
a.row1 - b.row3
a.row2 - b.row1
a.row2 - b.row2
a.row2 - b.row3
So if row 1 evaluates to true on multiple rows, then just add a distinct on a.Column1, etc.
If you want to use the return value from your function in an Oracle SQL statement, you will need to change the function to return 0 or 1 (or 'T'/'F' - some data type supported by Oracle Database, which does NOT support the Boolean data type).
Then you probably want something like
select <columns from A>
from A
where A.timezone = 1
and exists ( select *
from B
where B.timezone = 1
and ST_intersects(A.shape, B.shape) = 1
)
Lets say I have a schema:
key STRING NULLABLE
values INTEGER REPEATED
Now, please note that second column is a repeated field of integers,
Lets say the data is something like:
key:'abc'
values: 1 2 3 (3 separate values, same for below values)
key:'def'
values: 1 2 5
key:'ghi'
values: 1 6 9
And here I wish to find out keys which has values 1 and 2 ? Expecting 'abc' and 'def' as result set.
Looking for a query for this. I want an 'and' ('in' does not work here). I need those both values to be present for any key to return as result.
SELECT
key,
SUM(values = 1 or values = 2) WITHIN RECORD AS check
FROM yourtable
HAVING check = 2
My table structure is as follows :
id category
1 1&2&3
2 18&2&1
3 11
4 1&11
5 3&1
6 1
My Question: I need a sql query which generates the result set as follows when the user searched category is 1
id category
1 1&2&3
2 18&2&1
4 1&11
5 3&1
6 1
but i am getting all the results not the expected one
I have tried regexp and like operators but no success.
select * from mytable where category like '%1%'
select * from mytable where category regexp '([.]*)(1)(.*)'
I really dont know about regexp I just found it.
so please help me out.
For matching a list item separated by &, use:
SELECT * FROM mytable WHERE '&'||category||'&' LIKE '%&1&%';
this will match entire item (ie, only 1, not 11, ...), whether it is at list beginning, middle or end.