BigQuery Replacing NULL with a string - google-bigquery

I am new to BigQuery. I have the following simple query:
SELECT name, assetType,resource.data.shieldedInstanceConfig.enableSecureBoot FROM test001_99a3cr6b.assets_compute_googleapis_com_Instance;
It gives me the the following output:
Row name assetType enableSecureBoot
1 //compute.googleapis.com/projects/blah-ddi-test001-prod/zones/us-east4-b/instances/ddi-us-east4-b-5c2b51b2e3740 compute.googleapis.com/Instance null
2 //compute.googleapis.com/projects/blah-ddi-test001-prod/zones/us-east4-a/instances/ddi-us-east4-a-5c2b51b2e3740 compute.googleapis.com/Instance null
3 //compute.googleapis.com/projects/blah-ddi-test001-prod/zones/us-east4-a/instances/ddi-us-east4-a-f2f9a153c4590 compute.googleapis.com/Instance null
4 //compute.googleapis.com/projects/blah-admin-rbs-prod/zones/us-east4-c/instances/krp-pci-krp-vm-hx5q compute.googleapis.com/Instance true
5 //compute.googleapis.com/projects/blah-admin-rbs-prod/zones/us-east4-b/instances/krp-pci-krp-vm-bvt2 compute.googleapis.com/Instance true
6 //compute.googleapis.com/projects/blah-admin-rbs-prod/zones/us-east4-c/instances/orn-test-01 compute.googleapis.com/Instance true
7 //compute.googleapis.com/projects/blah-web-ingress-prod/zones/us-east4-c/instances/instance-1 compute.googleapis.com/Instance true
8 //compute.googleapis.com/projects/blah-network-hub/zones/us-central1-a/instances/orn-test-central1 compute.googleapis.com/Instance true
9 //compute.googleapis.com/projects/blah-network-hub/zones/us-east4-c/instances/orn-test-east4 compute.googleapis.com/Instance true
10 //compute.googleapis.com/projects/blah-network-hub/zones/us-east4-c/instances/test-vm compute.googleapis.com/Instance true
11 //compute.googleapis.com/projects/blah-network-hub/zones/us-central1-a/instances/test-vm-gui compute.googleapis.com/Instance true
12 //compute.googleapis.com/projects/blah-network-hub/zones/us-east4-c/instances/orn-test-east4 compute.googleapis.com/Instance true
I want to replace the values under field 'enableSecureBoot' with 'Not_Set' I tried IFNULL, as well as IF statements, but gives me unrecognized name 'enableSecureBoot' no matter what I do. Can you anyone give me a hint on how to do it, please?
Thanks so much!!

By looking at your SELECT statement, I think resource.data.shieldedInstanceConfig is a STRUCT in your table.
if you want to create a new column enableSecureBoot using the value of a STRUCT field resource.data.shieldedInstanceConfig.enableSecureBoot,
you could do below
SELECT
name,
assetType,
IFNULL(
CAST (resource.data.shieldedInstanceConfig.enableSecureBoot AS STRING),
'Not_Set'
) AS enableSecureBoot
FROM test001_99a3cr6b.assets_compute_googleapis_com_Instance;
Please note that the original resource.data.shieldedInstanceConfig.enableSecureBoot is a BOOLEAN type column, and the new enableSecureBoot is a STRING type column

This is what worked:
SELECT name, assetType,IFNULL(CAST(resource.data.shieldedInstanceConfig.enableSecureBoot AS STRING), 'NOT SET') AS ENABLED
FROM test001_99a3cr6b.assets_compute_googleapis_com_Instance;
Thanks again!
Alex

Related

How to return NULL records on a query with a where clause?

I'm using SQL Server 2016 and I have a view setup for novice end users.
To start, let's say there is a table like the following:
id number
=========
1 2
2 4
3 6
4 NULL
5 12
If a user makes a query on the view such as, select * from view1 where number <> 12, the view is setup to return NULL values as -99 using coalesce(number,-99):
Result of 'select * from view1 where number <> 12':
id number
=========
1 2
2 4
3 6
4 -99
Is there anyway to have the view return NULL instead of -99 (or whatever value), without the end user having to include ... or where is null in their query?
I understand NULLs and why it behaves like this, but for convenience I'd rather these end users not have to do this.
No.
The best you can do is fix the result so it decodes -99 as NULL:
SELECT id, CASE WHEN number = -99 THEN NULL ELSE number END AS number
FROM view1
WHERE number != 12
which I believe defeats the purpose of not exposing NULL values to the end user, or approach the data by accounting NULL as a valid data, using OR number IS NULL in that matter.
Try this:
select * from view1 where number <> 12 or number is null

Can you use an 'In' operator in a column expression?

Using Oracle 11. Trying to write some simple code for a conceptual demo. To illustrate what I'm trying to achieve, imagine I have SOMETABLE that has two columns: ID and NAME, like so:
ID NAME
---------
1 Tom
2 Larry
3 David
4 Steve
I'm trying to compute a third column that is true if the second column matches one of two hard-coded values. Something like this (which of course doesn't work.)
Select ID,
NAME,
(NAME in ('Larry', 'David')) as IS_FAVORITE
from SOMETABLE
and hoping to get this output...
ID NAME IS_FAVORITE
----------------------
1 Tom FALSE
2 Larry True
3 David True
4 Steve FALSE
Much to my surprise, I'm being told Oracle doesn't have the concept of booleans and I should be using 'numeric strings' or something like that, so this too is fine...
ID NAME IS_FAVORITE
----------------------
1 Tom 'N'
2 Larry 'Y'
3 David 'Y'
4 Steve 'N'
So can you use the IN operator in a column expression like this? If not, how would one compute the column that I am after?
You can achieve your expected output using case expression.
Select ID,
NAME,
CASE
WHEN
NAME in ('Larry', 'David')
THEN
'TRUE'
ELSE
'FALSE'
END as IS_FAVORITE
from SOMETABLE

Need to compare the cell values of same column of a table based on another column value in SQL

I need to compare the cells of a column of a table based on the value of another column value in sql.
Id A B
1 Ram 50
2 Ram 50
3 Siva 123
4 Siva 25
5 Rose 75
6 Rose 75
7 Siva 123
I have the above table, i need to check whether Ram in Column A has same value in column B, if not i should return false.
In the above case it should return false as Siva in(Column A) has different values in Column B.
I don't know how to do this. Kindly help on the same.
Result Expected
Return "False", as there is value mismatch for Siva
Here is one trick
select A,
case when min(B)=max(B) then 'True' else 'False' end as Flag
From yourtable
Group by A
or If you want to display the flag column for every row then
select A,
case when min(B)over(partition by A)=max(B)over(partition by A) then 'True' else 'False' end as Flag
From yourtable
Update : If you want to display False if at least one mismatch is present then
SELECT CASE
WHEN EXISTS(SELECT 1
FROM yourtable
GROUP BY A
HAVING Min(B) <> Max(B)) THEN 'False'
ELSE 'True'
END

unusual sql server query result

Let's say I have a table called nameAge:
ID Name Age
1 X 12
2 Y 12
3 null null
4 Z 12
and when I run a query like:
select * from nameAge where Age <> 12
it returns me an empty result set while I have row with id 3 where age is different than null?
Using Sql Server 2008 R2.
Any ideas?
Edit: Possibility to be duplicate with suggested answer may be at one point but does not cover at all and it shows how to use null values when compared with null but what I wanted to ask was about the result set which includes null values
This is the intended behavior. You cannot compare NULL values using = or <>. You have to use IS NULL or IS NOT NULL.
If you want NULL values only use IS NULL:
select * from nameAge where age IS NULL
If you want NULL values with age <> 12 values, use:
select * from nameAge where age <> 12 OR age IS NULL
The expression
WHERE NULL <> 12
does not return TRUE or FALSE, but actually returns UNKNOWN. This means that the third record in your table will not be returned by your query.
As #ughai mentioned, you should use IS NULL instead to query that record:
SELECT * FROM nameAge WHERE age IS NULL
Have a look at the Microsoft SQL Server documentation for more information.
When you are dealing with NULLs you should be always careful because of 3 valued logic used in Sql Server(when a predicate can be evaluated to TRUE, FALSE or UNKNOWN). Now here is a classic select statement where many newcomers make a mistake, suggesting that the statement would return all rows where Age <> 12 including NULLs.
But if you know the easy fact that comparing NULL to any value, even to NULL itself will evaluate to UNKNOWN it is getting more clear what is going on. WHERE clause will return ONLY those rows where predicate is evaluated to TRUE. Rows where predicate evaluates to FALSE or UNKNOWN will be filtered out from resultset.
Now let's see what is going on behind the scene. You have 4 rows:
ID Name Age
1 X 12
2 Y 12
3 null null
4 Z 12
and the predicate is:
where Age <> 12
When you evaluate this predicate for each row you get:
ID Name Age Evaluation result
1 X 12 FALSE --(because 12 <> 12 is FALSE)
2 Y 12 FALSE --(because 12 <> 12 is FALSE)
3 null null UNKNOWN --(because NULL <> 12 is UNKNOWN)
4 Z 12 FALSE --(because 12 <> 12 is FALSE)
Now remember that WHERE clause will return only rows where predicate evaluates to TRUE and it is clear that you will not get any result because no row evaluates to TRUE.

IS NULL function not working

I've tested IS NULL function provided by Google BigQuery, but it doesn't seems to give correct results.
SAMPLE DATA:
id age gender password
1, 11, NULL, NULL
1, 11, "NULL", "NULL"
1, 11, "null", "null"
1, , NULL, NULL
QUERY:
SELECT id, age, gender, password, id IS NULL, age IS NULL, gender IS NULL, password IS NULL
FROM privatedata.testnull
OUTPUT:
Row id age gender password f0_ f1_ f2_ f3_
1 1 11 NULL NULL false false false false
2 1 11 NULL NULL false false false false
3 1 11 null null false false false false
4 1 0 NULL NULL false false false false
But this SQL works:
SELECT NULL IS NULL, COUNT(*) FROM privatedata:testnull
So I'm not sure if IS NULL is working or not. Also I'm confused with how I would insert null data in string format and numeric format.
EDITED ANSWER:
For very old tables there was an issue where we did not preserve the between an empty field and a NULL one. This issue should by fixed for any table that was created after March, 2013.
OLD ANSWER:
I believe the issue is that for string fields, we're interpreting the NULL as the string "NULL". What if you just use , , for the string values?
The one that is surprising is that age in row 4 should be showing up as null. It looks like something is wrong with the IS_NULL function. I've filed a bug internally. The IS_EXPLICITLY_DEFINED() function, however, should return what you expect (e.g. IS_EXPLICITLY_DEFINED for age in row 4 would return fasle).