Update Table with Aggregate function in where clause Bigquery - google-bigquery

I have TableA with the following data
Name A B Final
Andy 1 1 2
Sam 1 0 2
I want to write an update query which will do the following:
If sum(A+B) <> Final then update row else do not Update
My Query
Update TableA
Set A = Case when Final in (1,2) then 1 else 0 End
Set B = Case When Final = 2 then 1 else 0
where final in (1,2) and **final <> sum(A+B)**
Since we can't use an aggregate function in update where clause I am not sure how to do the last part.
The query should only update row for Sam.
Thanks for you help!

Update TableA
Set A = Case when Final in (1,2) then 1 else 0 End
Set B = Case When Final = 2 then 1 else 0
where final in (1,2) and final <> A+B

Related

Is there a way to create a new column and assign values based on criteria on an existing column in a SQL query?

I want to create a new column in my query output and assign values to it based on existing columns in my input. Let's say i have column A in my input and want to create column B in my query output. How do I add the following logic in a sql select statement?
If A = 1 then set B to x
Else if A = 2 then set B to y
Else if A = 3 then set B to z
Else set B to null
I have to do this for multiple new columns in my output within the same query.
This is a case expression:
select . . .,
(case A when 1 then 'x' when 2 then 'y' when 3 then 'z' end) as b
Something like this?
SELECT A,
CASE
WHEN A = 1 THEN 'x'
WHEN A = 2 THEN 'y'
ELSE 'z'
END AS B
FROM ...

SQL AS query results in duplicate column

I have a SQL query like so
SELECT
name,
CASE WHEN (new_value=2) THEN 0 END as out,
CASE WHEN (previous_value=2) THEN 1 END as out
FROM my_table;
This results in duplicate columns:
name out out
foo 1 null
bar null 1
instead of
name out
foo 1
bar 0
How do I fix this?
You want one case expression with two conditions:
SELECT name,
(CASE WHEN new_value = 2 THEN 0
WHEN previous_value = 2 THEN 1
END) as out
FROM my_table;
Consider:
SELECT
name,
CASE
WHEN new_value = 2 THEN 0
WHEN previous_value = 2 THEN 1
END as out
FROM my_table;
In your query, each case expression generates one column in the resulset. You want only one, with two branches (denoted by when ... then ...)
You are getting null output, so you need to add else on this.
select name,
case
when new_value = 2 then 0
when previous_value = 2 then 1
else 0 end as out
from my_table;

how to update one record column to true, all else false

I am trying to update a column in a record to true to indicate that the record is the one active in the table. However, by updating this record, I must then update all other records for that column to false. Is there a way to do this in one SQL statement? I can do it in two statements like this:
UPDATE My_Table
SET is_group_active = 0
UPDATE My_Table
SET is_group_active = 1
WHERE group_id = 2;
You could use a case expression:
UPDATE my_table
SET is_group_active = CASE group_id WHEN 2 THEN 1 ELSE 0 END;
I would write this as:
UPDATE t
SET is_group_active = (CASE group_id WHEN 2 THEN 1 ELSE 0 END)
WHERE is_group_active <> (CASE group_id WHEN 2 THEN 1 ELSE 0 END);
Or perhaps:
UPDATE t
SET is_group_active = (CASE group_id WHEN 2 THEN 1 ELSE 0 END)
WHERE is_group_active = 1 OR group_id = 2
There is no need to update rows that already have the correct value. (Note: The logic would be slightly more complicated if is_group_active can take on NULL values).

Changing when to if statement in SQL server

How do I change the following code to an if statement that returns a boolean 0 or 1 value? My end results I would like to have, is one column listing the interest rate of 2, and my results column with a 0 or 1 if the condition is true.
(Case when new_interestratevariability = 2
and (new_interestrateindex = 1 or new_interestrateindex = 2 or new_interestrateindex = 3 or new_interestrateindex = 4 or new_interestrateindex = 6)
and new_crms_dt = #Curr_Date
then 0 else 1 end) as CIEDIT_VAL_96,
Currently, I am getting something like below:
Results Table
To filter rows, use a Where clause. The Case statement in the Select clause will modify the value shown on the row.
Select *
from table
Where new_interestratevariability = 2
and new_interestrateindex IN (1,2,3,4,6)
and new_crms_dt = #Curr_Date
Found my answer, it was as simple as adding "not in" instead of just "in". Thanks everyone
(Case when new_interestratevariability = 2
and (new_interestrateindex not in(1,2,3,4,6))
and new_crms_dt = #Curr_Date
then 1 else 0 end) as CIEDIT_VAL_96,

query sql returning custom value

I have a table with 3 columns (containing Integer values that assume only values from 0 to 10). I want extract, with a single query, a table with 1 column. This column must assume a value based on the following logic:
If one of these three columns has value 0 ----> the value of column of table generated by query must be 0 too.
If none of the last three columns has value 0 ----> the value of column must assume the value 1.
You are looking for CASE construct or IF function:
SELECT CASE WHEN (t.field1 = 0 OR t.field2 = 0 OR t.field3 = 0) THEN 0
ELSE 1 END AS value
FROM t;
In this specific case you might also use the fact that any member being zero will zero the product:
SELECT CASE WHEN (t.field1*t.field2*t.field3 = 0) THEN 0 ELSE 1 END AS value
FROM t;
Or
SELECT IF((t.field1*t.field2*t.field3)=0, 0, 1) AS value FROM t;
This is a simple case statement. Assuming there are no NULL values, try this:
select (case when col1 = 0 or col2 = 0 or col3 = 0 then 0 else 1 end)
Try this
SELECT
CASE
WHEN column1 = 0 THEN 0
WHEN column2 = 0 THEN 0
WHEN column3 = 0 THEN 0
ELSE 1
END
FROM urtable