COUNTing IS NULL returns 0 or with an Error [BigQuery] - sql

I want to find the number of data that is NULL but i do not know why this code returns an error: (Error Message said there was a problem with my WHERE)
SELECT first_review, COUNT (1) AS firstreviewisnull
FROM [table_name]
GROUP BY first_review
WHERE first_review IS NULL
I tried this code and the count returns to 0 - which I know is incorrect:
SELECT COUNT(first_review) AS firstreviewisnull
FROM [table_name]
WHERE first_review IS NULL
If possible, please help to:
explain what i did wrong
provide with the correct code
thank you!

SELECT COUNT(*) AS firstreviewisnull
FROM [table_name]
WHERE first_review IS NULL
Count() function ignores Null values (it does not count them). Use count(*) if you want to count the rows where girst_review is null.

Your SQL in the second image seems correct, assuming the [table_name] is actually replaced with the proper table name?
Also, check that the values for first_review are actually null, and not an empty String “”, which is not NULL.

Related

Can I know the issue of this SQL query

I have this SQL query:
but I'm getting an error:
If I remove the comma-separated value from the variable, it is working fine. As well as if I remove the NULL checking feature it is working fine. Can I know the issue of this
It's because a CASE WHEN can only return 1 value.
And a STRING_SPLIT returns a resultset.
I assume something like this is what you want.
SELECT *
FROM Facility f
WHERE (#Facility IS NULL OR f.facilityCode IN (SELECT value FROM string_split(#Facility,',')))
This will get all records if the variable is null.
The function dbo.split will split the string in more than one value. This will confuse your subquery and the error you are receiving will be thrown.
In case you need what goes before the comma consider using:
select top 1 value
from dbo.split(#Facility, ','))
You want to say if the variable is NULL so ignore the WHERE statement, if so your query would be:
SELECT *
FROM Facility f
WHERE #Facility IN (select value from dbo.split(#Facility, ',')) OR #Facility IS NULL

Count null values in a single column using hiveql

I would like to count the number of null values in a single column.
With:
select count(1)
from usedcars3
where model is NULL
I managed to get count of the null values in a single column in SQL however when I ran:
select count(1)
from usedcars3
where model is null;
in hive,
it gave me back 0 as the result.
I've searched around and haven't found anything as to why.
Any help/insight would be greatly appreciated.
select count(*) from usedcars3 where model is null;

Null query result cant fetch in Select query in Oracle

I have below query which i am trying to run but not returning the expected result. The ISIN field value which is Null in EXPORT_BB is also getting ignore and not showing the result with the condition given in NOT IN clause. The export_blacklist has only one row value and which is not Null but still i dont for what reason the null value is getting ignored.
Select * from EXPORT_BB where ISIN NOT IN
(
SELECT
ISIN
FROM
export_blacklist);
If i run only select query without the NOT IN clause then i can see values which is NULL for ISIN field.
JUst for test i tried below query and its also resulting nothing. Is it bug in Oracle 18c or something is missing?
select 'null is not in set' from dual where null not in (select 1 from dual);
Any comparison of NULL with =, <>, <, > or in a IN or NOT IN clause will return NULL, so that row is not included in the results (because only rows for which the returned value is TRUE will be included in the results).
Change your code with a condition for the case that ISIN is NULL:
SELECT * FROM EXPORT_BB
WHERE ISIN NOT IN (SELECT ISIN FROM export_blacklist)
OR ISIN IS NULL
NULL values doesn't work with NOT IN it's the normal behaviour.
You have to convert the NULL to another value to be able to operate with it or use IS NULL/IS NOT NULL
Select * from EXPORT_BB where NVL(ISIN, 999999) NOT IN
(
SELECT
NVL(ISIN, 999999)
FROM
export_blacklist);
Comparing to a null value in Oracle always returns false.
Is NULL >= 1? No.
Is NULL < 1? No.
Is NULL in your set? Regardless of what your set is, the answer is no.
Is NULL not in your set? Again, no.
It is the expected behaviour. NOt related to 18c it is the same way from Oracle 7 onwards
NOT IN doesnt consider nulls.
NOT EXISTS does consider nulls.
Consider the following example in db fiddle
https://dbfiddle.uk/?rdbms=oracle_18&fiddle=8be0a790d8172093a032602345038e8e
See a discussion on this
https://asktom.oracle.com/pls/apex/asktom.search?tag=in-vs-exists-and-not-in-vs-not-exists
As you have been answered by collegues you have to specify that you wanna return null values too.
Namely
SELECT *
FROM EXPORT_BB
WHERE ISIN NOT IN (SELECT ISIN FROM EXPORT_BLACKLIST)
OR ISIN IS NULL;

Selecting from table to insert into another, getting a type error

I have the following query which inserts data into one table after selecting it from another.
The problem is that the data types do not match for one of the columns. I have simplified the query below.
INSERT INTO tbl.LogTable (
[SelPartNo], -- This does not match, see below
)
SELECT TOP 1
IF([SelPartNo] = 'False', NULL, [SelPartNo],
FROM tbl.MyTable
WHERE ID = '20358'
ORDER BY CreateDate DESC
The first SelPartNo is an int and the second is a VarChar. In most instances the SelPartNo for the second one (tbl.MyTable) is NULL or an integer, which I don't think will cause a problem. But in some cases the value is "False", which needs to return NULL.
I have tried an IF statement but I am doing something wrong because it's giving a syntax error and I am unsure if this is the correct approach.
Your code is syntactically incorect...
Try it with
NULLIF([SelPartNo],'False')
This function returns NULL if the two expressions are equal.
Details: https://msdn.microsoft.com/en-us/library/ms177562.aspx
I don't think IF is a function, at least not one which you can use in a SELECT statement. But CASE WHEN ... END is your friend:
INSERT INTO tbl.LogTable (
[SelPartNo]
)
SELECT TOP 1
CASE WHEN [SelPartNo] = 'False' THEN NULL ELSE [SelPartNo] END
FROM tbl.MyTable
WHERE ID = '20358'
ORDER BY CreateDate DESC

How to select an empty result set?

Want to improve this post? Provide detailed answers to this question, including citations and an explanation of why your answer is correct. Answers without enough detail may be edited or deleted.
I'm using a stored procedure in MySQL, with a CASE statement.
In the ELSE clause of the CASE ( equivalent to default: ) I want to select and return an empty result set, thus avoiding to throw an SQL error by not handling the ELSE case, and instead return an empty result set as if a regular query would have returned no rows.
So far I've managed to do so using something like:
Select NULL From users Where False
But I have to name an existing table, like 'users' in this example.
It works, but I would prefer a way that doesn't break if eventually the table name used is renamed or dropped.
I've tried Select NULL Where False but it doesn't work.
Using Select NULL does not return an empty set, but one row with a column named NULL and with a NULL value.
There's a dummy-table in MySQL called 'dual', which you should be able to use.
select
1
from
dual
where
false
This will always give you an empty result.
This should work on most DBs, tested on Postgres and Netezza:
SELECT NULL LIMIT 0;
T-SQL (MSSQL):
SELECT Top 0 1;
How about
SELECT * FROM (SELECT 1) AS TBL WHERE 2=3
Checked in myphp, and it also works in sqlite and probably in any other db engine.
This will probably work across all databases.
SELECT * FROM (SELECT NULL AS col0) AS inner0 WHERE col0 IS NOT NULL;
SELECT TOP 0 * FROM [dbo].[TableName]
This is a reasonable approach to constant scan operator.
SELECT NULL WHERE FALSE;
it works in postgresql ,mysql, subquery in mysql.
How about this?
SELECT 'MyName' AS EmptyColumn
FROM dual
WHERE 'Me' = 'Funny'
SELECT * FROM (SELECT NULL) WHERE 0
In PostgreSQL a simple
SELECT;
works. You won't even get any columns labeled 'unknown'.
Note however, it still says 1 row retrieved.