sql update multiple columns with multiple conditions - sql

I want to update 5 columns from another table. 2 of the column will be up dated from a condition the other 3 will also be update if condition one is true and condition two is true. is there a way to do this in one statement?
update tableNew
set
column1 = t1.column1
column2 = t1.column2
column3 = t1.column3
column4 = t1.column4
column5 = t1.column5
where t1.column10 in ('value1','value2')
from tableOld t1
where t1.column8 is null
i hope you can understand what I need from this example if not I can try and give more!!

Related

Joining Two Tables in BigQuery Without Common Column and Different Amount of Rows

I'm trying to create a table in BigQuery by joining two tables without a common column and differing row totals. Please help me update the query below or let me know if the logic doesn't make sense.
Table 1 = 180M rows
Table 2 = 65M rows
CREATE OR REPLACE TABLE `myproject.mydataset_joinedtable` AS
(SELECT
t1.column1 AS column1
t2.column2 AS column2
t1.column3 AS column3
t1.column4 AS column4
FROM `myproject.mydataset_table1` t1,`myproject.mydataset_table2` t2):

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

sql oracle using update

I need to take data from one table to add to another table.
I got two SQL's:
SQL1:
select * from table1;
SQL2:
select * from table2 where coloumn1 = table1.coloumn6 (number)
table1 looks like:
For better understanding we call each coloumn coloumn1, coloumn2, ... coloumn9
table2 looks like:
For better understanding we call each coloumn coloumn1, coloumn2, ... coloumn13
What should happen in text
My SQL has to take the value from table1.coloumn6 (number) - checking if this value is given in table2.coloumn1
select * from table2 where coloumn1 = table1.coloumn6 (SQL2)
If yes it should update the data from table1.coloumn2 (varchar2) to table2.coloumn4 (varchar2).
If I understand correctly, you want to update table1.column2 to the corresponding value in table2.column1 based on other conditions. Based on your description:
update table1
column2 = (select column4 from table2 t2 where t2.column1 = table1.column6)
where exists (select 1 from table2 t2 where t2.column1 = table1.column6);
MERGE is good for updating/inserting tables based on other tables. Something like this should work (would be a bit more clear with distinctly-named columns). It also accepts an optional 'WHEN NOT MATCHED THEN' clause which lets you insert new records.
MERGE INTO table2
USING (SELECT column2, column6 FROM table1) table1
ON (table2.column1 = table1.column6)
WHEN MATCHED THEN
update set column4 = column2;

SQLite. "UPDATE table1 SET column1 = (SELECT column2 FROM table2);" is not working

Why this construction is not working in SQLite:
UPDATE table1 SET column1 = (SELECT column2 FROM table2);
where column1 and column2 has the same number of rows and the same type.
Use the common column to look up the matching record:
UPDATE table1
SET column1 = (SELECT column2
FROM table2
WHERE table2.Name = table1.Name)
It is not going to work because the table1.column1 only wants one value. What you are actually doing is setting table1.column1 = all the results from table2.column2.
You will probably have to join the two tables together on a common column or key
Because nested select returns more than one row probably. You try to put all the data from table2 into 1 element in table1
Is Column1 just getting the same value all the way down the column? Otherwise, you need to do a JOIN in the update here and Set Column1 = Column2 based on the join.
You are not linking the two tables
so it is trying to put all column2 values into each table1 row.
Link the two together with a field such as
Update table1 set column1 = column2
from table2 where table1.ColumnA = tables2.ColumnA
The reason why your statement isn't working is because you're telling it to put all of the values for column2 into every column1 cell in table1. You want something more like:
UPDATE
table1
SET
table1.column1 = table2.column2
FROM
table1
INNER JOIN
table2
ON
table1.id = table2.table1_id

SQL Replace used during a compare

I currently run this query:
UPDATE table1
SET column1 = table2.columnA
FROM table2
WHERE column2 = table2.columnA
AND column3 = table2.columnC
Yes there is a duplicate column, sorry.
When I cross reference column2 with table2.columnA there is a chance to get NULL because column2 DOES NOT contain / where as table2.columnA may contain /
I don't want to change the data in table2.columnA
It is my understanding that I can run this query to REPLACE the characters.
SELECT REPLACE ([table2.columnA],'/','-')
FROM table2
It is necessary that this does not make a permanent change to table2 so I want to make sure that I am getting this right or to see if there is a better way.
Now I want to combine the two queries but am unsure how.
UPDATE table1
SET column1 = table2.columnA
FROM table2
WHERE column2 = (SELECT REPLACE([table2.columnA],'/','-')table2.columnA)
FROM table2
AND column3 = table2.columnC
Thanks for the help!
Something like
UPDATE table1
SET column1 = table2.columnA
FROM table1
inner join table2
on table1.column2 = REPLACE([table2.columnA],'/','-')
and table1.column3 = table2.columnC