Update table field from a different table with if statement - sql

I have two tables as;
Table 1
[SKU], [QTY_on_hand]
Table 2
[XREF] , [QTY_on_hand]
I want to update Table 2 's [QTY_on_hand] with Table 1 's [QTY_on_hand]
where [SKU]=[XREF] and if Table1's [QTY_on_hand] is greater than "30" else table2 's [QTY_on_hand] = '0'.
So the T-SQL is:
update Table 2
set [QTY_on_hand] = t.[QTY_on_hand]
from Table 1 t
where t.[SKU] = [XREF]
But the if statement is missing here and I couldn't manage to how to integrate if statement with 'case when' or 'if-else'.

Follows is how to join a second table to the table being updated in an update statement and use a conditional expression for updating a value:
update T2 set
qty_on_hand = case when T1.qty_on_hand > 30 then T1.qty_on_hand else 0 end
from Table2 T2
inner join Table1 T1 on T1.SKU = T2.XREF
where ?? -- surely you don't want to update the entire table?

Have you tried a case expression like this
UPDATE Table2
SET Qty_on_Hand = CASE WHEN SKU=XREF AND Qty_on_Hand > 30 THEN Table1.Qty_on_Hand ELSE Table2.Qty_on_Hand END

Related

Bigquery: Update column based on condition set in another table

I have two tables t1 and t2. I need to join them and set the blocked column value in t1 as 'X' when t2.inact = '' (is empty) and t2.objval = 1000. Both the tables are joined by obid.
Table t1:
obid
blocked
1
2
Table t2:
obid
inact
objval
1
1000
2
2000
Expected output: The table t1 should now look like this
obid
blocked
1
X
2
In bigquery it is said that it is not possible to use WITH CTE's along with update statement which was my first try..What could be the other way possible? Below is my another SQL attempt with CASE and this is creating a new column called blocked...but the requirement is filling the data in already present column blocked.
WITH abc AS(
SELECT obid,blocked
FROM table1),
def AS (
SELECT obid,inact,objval,
FROM table2
WHERE objval = '1000')
SELECT CASE WHEN t2.inact = '' THEN 'X'
ELSE '' END as blocked
FROM abc t1
JOIN def t2
ON t2.obid = t1.obid
Any help appreciated!!!
You can still use an UPDATE statement on the "t1" table, while checking conditions on the "t2" table, thus simulating a join between "t1" and "t2".
UPDATE t1
SET blocked = 'X'
FROM t2
WHERE t1.obid = t2.obid AND t2.inact = '' AND t2.objval = 1000

Using a trigger to insert values into another table unless value exists in which case update

I have a table (Table1) which has three columns: 'ID' , 'Status' , 'Title'. I have second table (Table2) that has three columns: 'ID' , 'Table1ID' , 'Title'.
ID is a sequential number value, title is an alphanumeric value and status have values of either 0 or 1. Starting with a value of 0 when the record is created in Table1.
What I'm trying to achieve is a trigger: that when [Table1].Status changes from value 0 to 1 then to insert a new row into [Table2] with the value of [Table1].ID into [Table2].Table1ID and [Table1].Title into [Table2].Title of only the updated record in [Table1].
However if [Table2].Table1ID already has a matching value of [Table1].ID on the updated value then instead of inserting a new row it should simply update the value of [Table2].Title.
Here's what I've attempted so far:
Create Trigger [dbo].[test]
ON [dbo].[Table1]
After Update
As Begin
Set NOCOUNT ON;
IF UPDATE (Status)
Begin
INSERT INTO [Table2]
VALUES ((SELECT DISTINCT ID FROM INSERTED WHERE NOT EXISTS (SELECT * FROM TABLE2 WHERE TABLE2.TABLE1ID = INSERTED.ID)))
END
END
Go
If I understand all the requirements I think you need something like this. First it will update Table2 only when the status changes. Since you are updating the Title I would think you would update it when the value of Title changes but that isn't what you stated you want.
Then it will insert any rows that don't exist already.
Please notice that this will work no matter how rows get updated in a single update statement.
Create Trigger [dbo].[test]
ON [dbo].[Table1]
After Update
As Begin
Set NOCOUNT ON;
--first we need to update any existing rows in Table2
update t2
set Title = i.Title
from inserted i
join deleted d on d.ID = i.ID
join Table2 t2 on t2.ID = i.ID
where d.Status = 0 --only where the row in Table1 has a status of 0
and i.Status = 1 --only when the new value has status = 1
--now we can insert any rows that don't already exist into Table2
INSERT INTO [Table2]
(
ID
, Title
)
SELECT i.ID
, i.Title
FROM INSERTED i
WHERE NOT EXISTS
(
SELECT *
FROM TABLE2
WHERE TABLE2.TABLE1ID = i.ID
)
END

Most Elegant Way to Populate Nulls from Reference Table

I have two tables like so:
Table 1:
Column 1 Column 2
A 1
B NULL
C 3
D 4
Table 2:
Column 1 Column 2
A 1
B 2
C 3
D 4
I receive the data in table 2 on a time delay, and what I need to do when I receive it is populate the null values in Table 1 with the data in table 2. Is there an elegant way to do this in Standard SQL without making temporary tables or using sub-queries?
Thanks!
Consider using a MERGE statement:
MERGE dataset.table1 AS t1
USING dataset.table2 AS t2
ON t1.column1 = t2.column1
WHEN MATCHED THEN
UPDATE SET t2.column2 = t1.column2
Take a look at the supported actions you can perform; if you want to insert new rows, you can do that using MERGE as well.
You can use the UPDATE statement from the DML (Data Manipulation Language) and update Table 1 in place.
It is not clear from your question how you receive data from Table 2, so I will assume that you have a temporary table called Table2. You will have to JOIN Table1 and Table2 in order to find which rows have NULL values, i.e Table1.Column2 IS NULL. Then update Column2 in those rows using the value in Table2. It should be something like this:
UPDATE Table1
SET Column2 = Table2.Column2
FROM
Table1 LEFT JOIN Table2 USING(Column1)
WHERE
Table1.Column2 IS NULL
Correct use of UPDATE statement would be
UPDATE `project.dataset.table1` a
SET Column_2 = b.Column_2
FROM `project.dataset.table2` b
WHERE a.Column_1 = b.Column_1
AND a.Column_2 IS NULL

Postgresql Update using Inner Join set

I have two tables that they share two fields (myfield1, myfield2) and one of the two tables has 2 other fields of interest.
Here is what I want to do:
1. Inner Join the two tables
2. Update a column (field1) in Table2 with either fields (afield or anotherfield) from the other table depending on afield is null or not.
The code below runs fine but the targeted set field (field1) doesn't get updated with anything.
Update Table2
Set field1 = (
CASE
WHEN os.afield is not null
THEN (os.afield)
Else os.anotherfield
End
)
from Table1 os
inner join Table2 fd
ON fd.myfield1= os.myfield1
AND fd.myfield2 = os.myfield2;
Update Table2 fd
Set fd.field1 =
(select CASE WHEN os.afield is not null THEN (os.afield) Else os.anotherfield End
from Table1 os
where fd.myfield1= os.myfield1
AND fd.myfield2 = os.myfield2);
It's called a correlated subquery which is executed for each row in Table2. But you must be sure that subquery returns single or zero rows.
This query will update all rows in Table2 if you want to update only those rows which exist in Table1 you need a WHERE
Update Table2 fd
Set fd.field1 =
(select CASE WHEN os.afield is not null THEN (os.afield) Else os.anotherfield End
from Table1 os
where fd.myfield1= os.myfield1
AND fd.myfield2 = os.myfield2)
where exists (
select 1 from Table1 os
where fd.myfield1= os.myfield1
AND fd.myfield2 = os.myfield2);

sql case statement update based on other table

I want to update a table (table1) based on the values of one or more fields in another table (table2). I believe this should be a case statement but I'm unsure how to incorporate a case statement and an update clause based on another table in one statement. Here's what I have so far which I know does not work:
update table1 i, table2 s
set i.sales = 'F'
where s.payment = 'Y'
and i.order_no = s.order_no;
I know how to do a select based on two tables but that's not very helpful since I don't want to create a new database object - I just want to update an existing object (table1):
create or replace view merge as
select
i.order_no
, case when s.payment = 'Y'
then 'F'
end as sales
from table1 i, table2 s
where i.order_no = s.order_no;
And I know how to update WITHIN a case statement:
UPDATE table1
SET sales = (
SELECT CASE
WHEN foo = 'X'
THEN 'F'
ELSE null
END
FROM table1
)
;
I considered a where clause instead of a case statement but it ends up selecting EVERY record and the 2nd table definitely has different values in the payment field:
update t1
set sales = 'F'
where exists (select table2.payment
from table2
where table2.order_no = table1.order_no
and table2.payment = 'Y');
try this:
update table1 i
set i.sales = (select case when x.payment = 'Y'
then 'F'
else i.sales end
from table2 x
where x.order_no = i.order_no);
I don't have an oracle running right now (so I cannot check the syntax propertly), but I think you can try something like this:
update table1 i
set i.sales = 'F'
where i.order_no IN (select s.order_no from table2 s where s.payment = 'Y')
Hope it helps!