BigQuery: How to add a new calculated column? - sql

I’m new to SQL so apologies if this is a basic question. I have a table in BigQuery that I’d like to add a new column to based on another.
The table has Column A with number values of FLOAT, I want to create Column B which will be False if Column A is less than 1.0 or True if it is greater. How exactly could I go about that?

Use if:
IF(columnA < 1, False, True)

TRY this
ALTER TABLE BigQuery
ADD B varchar(10) NULL
CONSTRAINT df_BigQuery DEFAULT
(CASE WHEN A < 1.00 THEN 'TRUE' else 'FALSE' END)

You can use below
select *, columnA < 1 as new_column
from your_table
Note: you do not need to use IF ... or CASE ... because columnA < 1 by itself already returns either true or false (with exception when columnA is null)

Related

I want to update a column of the table if value is not null in oracle update statement?

I want to update a column of the table if the value is not null and if value is null then do not update that particular column and other columns should be updated in oracle update statement . Is there any simple way to achieve this in sql query ?
Looks like CASE expression might help.
update your_table set
that_column = case when that_column is not null then 'new value'
else null
end,
another_column = 123,
yet_another = 'ABC'
where ...

Finding sum of empty columns in a row

I want to create a column which finds count of non empty columns in a postgres table (like given below). Can u give me a head's up or a solution?
You can use GENERATED always as column. Example:
create table test (a VARCHAR (50),
b VARCHAR (50),
c VARCHAR (50),
cnt numeric GENERATED always as (
case when a is null then 0 else 1 end +
case when b is null then 0 else 1 end +
case when c is null then 0 else 1 end
) STORED);
Link: https://www.db-fiddle.com/f/fU5LLuh5gvwkKhhRbeCyh8/0
In Postgres, I would simply do:
select t.*,
((a is not null)::int + (b is not null)::int + (c is not null)::int) as cnt
from t;
If you don't want to hard-code the column names, you can use some JSON magic for this:
select a,b,c,
(select count(*) from jsonb_object_keys(jsonb_strip_nulls(to_jsonb(t)))) as num_not_null
from the_table t
jsonb_strip_nulls(to_jsonb(t)) converts the whole row into a single json value where the column names are the keys. Any null value will be removed. Then the keys are extracted using jsonb_object_keys and the number of keys returned is the number of non-null columns in that row.
Using a CASE expression as shown in EragonBY's answer or the boolean expressions in Gordon's answer will be a lot faster if you don't mind explicitly listing all column names.
Online example

How to update table column in SQL Server only wherever null appears?

I have a SQL Server table with more than 15 columns.
One of my column name is verification_Status. Currently, I have 0, 1, 2, null values under verification_Status as below:
For example:
Id Name verification_Status
1 John 0
2 Kat 1
3 Williams Null
4 Rosy null
I want to make 0 wherever null appears. I have 4k rows to update so I am little worried.
update masterTable
set verification_Status = 0
where verification_Status == null
I am planning to use above query.
May I know this is the right query to my problem? Can someone guide me on this?
Use this:
update masterTable
set verification_Status = 0
where verification_Status is null
<> is Standard SQL; != is its T-SQL equivalent. Both evaluate for values, which NULL is not -- NULL is a placeholder to say there is the absence of a value.
Which is why you can only use IS NULL / IS NOT NULL as predicates for such situations.
This is standard of all kinds of SQL management platforms
refer this: Null (SQL)
Your query should be
update masterTable set verification_Status=0 where verification_Status is null
Because NUll=NUll alwaysFalse. So the records which will have value as NULL in verification_status will not update zero. so you should use Verification_status is null at where clause.
Periodic update of value:
update masterTable set verification_Status=0 where verification_Status is null
Permanent Default Value on any table insert:
ALTER TABLE {TABLENAME}
ADD {COLUMNNAME} {TYPE} {NULL|NOT NULL}
CONSTRAINT {CONSTRAINT_NAME} DEFAULT {DEFAULT_VALUE}
[WITH VALUES]
Check number of rows will affect before updating.
Select Count(*) from masterTable WHERE ISNULL(verification_Status,'') = ''
It is possible you also have empty string instead of null value in some columns.
If you want to update empty values and null values then use this.
UPDATE masterTable SET verification_Status=0 WHERE ISNULL(verification_Status,'') = ''

Case statement inside create-table

How do you use the "java if-statement" in SQL => PostgreSQL, while creating a table/Column?
CREATE TABLE Store(
Discount INT
AS CASE
WHEN SOLD_Amount>100000 THEN 2
WHEN SOLD_Amount>500000 THEN 5
WHEN SOLD_Amount>1000000 THEN 10
ELSE 0
END
NOT NULL)
This is probally wrong, please tell us, the community how to do this kind of action.
What you are looking for here is a computed column, which is not directly supported by Postgres. You could implement this in a view, like so:
CREATE VIEW someview AS
SELECT SOLD_Amount,
CASE
WHEN SOLD_Amount>100000 THEN 2
WHEN SOLD_Amount>500000 THEN 5
WHEN SOLD_Amount>1000000 THEN 10
ELSE 0
END As Discount
Or you could use a trigger to populate the column on insert/update.
You can use a special PostgreSQL feature: "generated" columns.
Based on an existing table, say:
CREATE TABLE store (sold_amount int, ...):
You could create this special function:
CREATE FUNCTION store_sold_amount(rec store)
RETURNS int LANGUAGE SQL IMMUTABLE
AS
$func$
SELECT CASE
WHEN rec.sold_amount > 100000 THEN 2
WHEN rec.sold_amount > 500000 THEN 5
WHEN rec.sold_amount > 1000000 THEN 10
ELSE 0 END;
$func$;
Then you can query:
SELECT s.amount, s.store_sold_amount
FROM store s;
More under these related questions:
How can I create a column in postgres from values and selections based on other columns?
Store common query as column?
SELECT A.*,
CASE
WHEN B.Table2 IS NOT NULL
THEN 'Yes'
ELSE 'No'
END AS results_column_name_here
INTO new_table_name
FROM Table1 A
LEFT JOIN Table2 B
ON A.col_to_join_on = B.col_to_join_on

Populate New Column with Existing Column Based On Criteria

I am selecting into an existing column C from column A if it has a value greater than zero. If not then I want to populate it with column B. It will populate with column A only if column B is zero. If both column A and B have values it always uses B. Any help would be appreciated.
(
CASE
WHEN column A > 0 THEN column A
ELSE column B
END
)
My insert into was correct but my select had the columns reversed which was causing what I thought was an error. Thanks for all of your help. What a noob thing to do.
Your database of choice will be extremely useful, as it looks like you are already correct in most RDBMS. Just stick exactly what you have already inside of a select:
--Insert based on select
--INSERT INTO [TABLETOINSERT] (ColumnToInsert)
SELECT
CASE
WHEN ColumnA > 0 THEN ColumnA
ELSE ColumnB
END AS NewColumn
--Or create a table the select
--INTO [NEWTABLENAME]
FROM [TABLENAME]
Or update:
UPDATE [TABLENAME]
SET NewColumn =
CASE
WHEN ColumnA > 0 THEN ColumnA
ELSE ColumnB
END
Assuming you want to update the value permanently and not just change it during select, do:
update MyTable
set ColumnC = CASE
WHEN ColumnA > 0 THEN ColumnA
ELSE ColumnB
END