NULL behavior with Comoperator like ALL in Oracle SQL - sql

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.

Related

SQL How to return a result set that will combine multiple rows into one row

I am trying to get one row returned for each store number and per date that includes all values from the RecordTypeA column for that date.
The table I am using is created with a column named "RecordTypeA", it is a bit data type with (1 and 0) entries. 1 equals Type A and 0 equals Type B.
What I am trying to do is show the value of the RecordTypeA column for the store if there are entries of 1 and / or 0 on the same date on the same row.
Scenario 1 (One row returns for the store for the date): RecordTypeA column value = '1'
There is one row in the table for the store and date and the RecordTypeA column = '1' :
Scenario 2 (Two Rows return for the store for the same date):
Row One - RecordTypeA = '1'
Row Two - RecordTypeA = '0' (The column is still named RecordTypeA, but value '0' means something different so I want to create a column name?)
Scenario 3 (One row returns for the store for the date):
RecordTypeA column value = '0'
There is one row in the table for the store and date and the RecordTypeA column = '0' :
My issue is that I am getting multiple rows returned when the store has a RecordtypeA = 0 and a RecordtypeA = 1 row. Which I need to return on the same row. (Create columns that hold both 1 and 0 or Null.
What I am getting is
StoreID Date RecordTypeA
1234 2020-01-04 0
1234 2020-01-05 0
1234 2020-01-05 1
Needed:
StoreID Date RecordTypeA RecordTypeB
1234 2020-01-04 0 NULL
1234 2020-01-05 0 1
I have tried adding in case statements but I have not been able to get the one row as needed. Also, searched and tried PIVOT statements (I don't truly understand PIVOTs) but I get an error on the RecordTypeA Bit type.
Case when s.RecordTypeA = '1' Then 'TypeA' Else 'Null' End as Type
Case when s.RecordTypeA = '0' Then 'TypeB' Else 'Null' End as Type
SELECT r.StoreID,
r.CreatedDate,
s.RecordTypeA
From Request r
Inner Join Stores s on r.id = s.id
Group by r.StoreID,
r.CreatedDate,
s.RecordTypeA
Welcome to stack overflow community!
Have you tried to construct 3 queries and use UNION ALL statement?
You can create a query for StoreID, Date, RecordTypeA, TypeB and Typec.
For example:
SELECT CONCAT(r.StoreID, r.CreatedDate, s.RecordTypeA) AS DATA, 'A' AS TYPE
FROM YOURDATABASE.YOURTABLE
WHERE (A CRITERIA FOR TYPE A)
UNION ALL
SELECT CONCAT(r.StoreID, r.CreatedDate, s.RecordTypeA) AS DATA, 'B' AS TYPE
FROM YOURDATABASE.YOURTABLE
WHERE (A CRITERIA FOR TYPE B)
UNION ALL
...
I use the same alias on the example because with union all, all the queries must have the same columns, so you can use a CONCAT to put all your data from the different queries in one column, the column "TYPE" is for the difference the queries at the result.
Even if you not use concat you can return the columns different but all the queries must have the same column count on the select.
With multiple queries, you can define de criteria you want for type A, B, C through Z but have all of them at one result.
Important: Union all statements are somehow heavy for performance, so have it in mind.

SQL query -- not getting expected result using ALL and '=' operator

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.

Filter rows based on multiple condition in where statement in PostgreSQL

I have following table in Postgres 11.0
id status
NCT00632827 true
NCT00632827 (null)
NCT00770185 false
NCT00770185 (null)
NCT00784303 (null)
NCT00784303 (null)
The id values are duplicated due to different value in status column. I would like to keep the rows with either true or false. If multiple rows have null value, I would like to keep only 1 row. The desired output is:
id status
NCT00632827 true
NCT00770185 false
NCT00784303 (null)
I am trying following query:
select id, status
where status = 'false' or status = 'true'
from table
How can I keep rows with null value (last row in the desired output)?
For this dataset, simple aggregation seems good enough to produce the expected results:
select id, bool_or(status) status from mytable group by id

Filtering a column based on having some value in one of the rows in SQL or Presto Athena

I am trying in Athena to output only users which have some specific value in them but not in all of the rows
Suppose I have the table below.
I want all users which have value '100' in at least one of their rows but also having in other rows value different than 100.
user | value
A | 1
B | 2
A | 100
D | 3
A | 4
C | 3
C | 5
D | 100
So in this example I would want to get only users A and D because only them having 100 and none 100.
I tried maybe grouping by user and creating an array of values per user and then checking if array contains 100 but I don't manage doing it presto.
Also I thought about converting rows to columns and then checking if one of columns equals 100.
Those solutions are too complex? Anybody knows how to implement them or anyone has a better simpler solution?
The users that have at least one value of 100 can be found with this SQL:
SELECT DISTINCT user
FROM some_table
WHERE value = 100
But I assume you are after all tuples of user and value where the user has at least one value of 100, this can be accomplished by using the query above in a slightly more complex query:
WITH matching_users AS (
SELECT DISTINCT user
FROM some_table
WHERE value = 100
)
SELECT user, value
FROM matching_users
LEFT JOIN some_table USING (user)
You can use sub query as below to achieve your required output=
SELECT * FROM your_table
WHERE User IN(
SELECT DISTINCT User
FROM your_table
WHERE Value = 100
)
If you just want the users, I would go for aggregation:
select user
from t
group by user
having sum(case when value = 100 then 1 else 0 end) > 0;
If 100 is the maximum possible value, this can be simplified to:
having max(value) = 100

Find certain values and show corresponding value from different field in SQL

So I found these 2 articles but they don't quite answer my question...
Find max value and show corresponding value from different field in SQL server
Find max value and show corresponding value from different field in MS Access
I have a table like this...
ID Type Date
1 Initial 1/5/15
1 Periodic 3/5/15
2 Initial 2/5/15
3 Initial 1/10/15
3 Periodic 3/6/15
4
5 Initial 3/8/15
I need to get all of the ID numbers that are "Periodic" or NULL and corresponding date. So I want a to get query results that looks like this...
ID Type Date
1 Periodic 3/5/15
3 Periodic 3/6/15
4
I've tried
select id, type, date1
from Table1 as t
where type in (select type
from Table1 as t2
where ((t2.type) Is Null) or "" or ("periodic"));
But this doesn't work... From what I've read about NULL you can't compare null values...
Why in SQL NULL can't match with NULL?
So I tried
SELECT id, type, date1
FROM Table1 AS t
WHERE type in (select type
from Table1 as t2
where ((t.Type)<>"Initial"));
But this doesn't give me the ID of 4...
Any suggestions?
Unless I'm missing something, you just want:
select id, type, date1
from Table1 as t
where (t.type Is Null) or (t.type = "") or (t.type = "periodic");
The or applies to boolean expressions, not to values being compared.