I am new to SQL and I want to do an UPSERT into a table with select from another table. For example I have 2 tables
TABLE1
ID DATE VALUE
1 23.09.2020 abc
2 01.02.2020 def
TABLE2
ID VALUE ADDRESS
1 xyz mmm
2 zzz nnn
2 zzz ppp
3 ccc qqq
The task is - If the ID in TABLE1 is of DATE = '23.09.2020' and ID is present in TABLE2 then update VALUE column in TABLE1 with the VALUE in TABLE2. And if ID in TABLE1 is present in TABLE2 but does not have DATE as '23.09.2020' then insert distinct (ID,VALUE) from TABLE2 into TABLE1. And if ID in TABLE1 is not present in TABLE2 then do nothing. So final result in TABLE1 after UPSERT should look like.
TABLE1
ID DATE VALUE
1 23.09.2020 xyz
2 01.02.2020 def
2 23.09.2020 zzz
NOTE : ID column is not a primary key, and I cannot make it as primary key.
I have tried something like below, but getting error and not able to achieve desired result.
upsert TABLE1(ID,DATE,VALUE)
SELECT DISTINCT ID,'23.09.2020',VALUE FROM TABLE2
WHERE TABLE1.ID = TABLE2.ID AND TABLE1.DATE = '23.09.2020'
UPDATE - I tried using MERGE as suggested, but getting ID = 2 inserted twice in TABLE1, where as I want it inserted only once as distinct (ID,VALUE) from TABLE2. Below is the MERGE query I tried.
MERGE INTO TABLE1 T1
USING TABLE2 T2 ON T1.ID = T2.ID AND T1.DATE = '23.09.2020'
WHEN MATCHED THEN
UPDATE SET T1.VALUE = T2.VALUE
WHEN NOT MATCHED THEN
INSERT(ID,DATE,VALUE) VALUES(T2.ID,'23.09.2020',T2.VALUE);
Result I am getting
TABLE1
ID DATE VALUE
1 23.09.2020 xyz
2 01.02.2020 def
2 23.09.2020 zzz
2 23.09.2020 zzz --> Duplicate, not wanted.
Result I want
ID DATE VALUE
1 23.09.2020 xyz
2 01.02.2020 def
2 23.09.2020 zzz
How can I insert distinct of (ID,VALUE) from TABLE2 using MERGE query ?
Could you try with below,
You have almost done everything right as I see, what I did use a sub query to select the distinct ID and VALUE and then join.
MERGE INTO TABLE1 T1
USING (SELECT DISTINCT ID,VALUE
FROM TABLE2 T2
) T2
ON ( T1.ID = T2.ID
AND T1.DATE = '23.09.2020')
WHEN MATCHED
THEN
UPDATE SET T1.VALUE = T2.VALUE
WHEN NOT MATCHED
THEN
INSERT(ID,DATE,VALUE)
VALUES(T2.ID,'23.09.2020',T2.VALUE);
Related
For example I am having two tables with id,age,status and height. And there is a table RESULT which I need to merge to.
Table 1
*id age status*
1 15 1
2 16 1
3 17 0
Table 2
*id height*
1 160
2 170
3 180
And Result table is:
Result table
*id age height*
1 15 160
I need to insert into Result table id,height,age from Table 1 join Table 2 on ID ,where status is 1.
How can I write something like
Merge into Result
USING(Select ... from Table1
join Table2 on Table1.id=Table2.id where status=1)
When Not Matched THEN
Insert into Result VALUES(Table1.id,age,height)
I need to get
RESULT
*id age height*
1 15 160
2 16 170
So how can I implement that merge which will find user with id=2 in Result
Table and Insert and will not Insert user with id=1 because it is already in table?
Try this:
MERGE INTO RESULT R USING (
SELECT
T1.ID,
T1.AGE,
T1.STATUS,
T2.HEIGHT
FROM
TABLE1 T1
JOIN TABLE2 T2 ON T1.ID = T2.ID
WHERE
STATUS = 1
) DATAA
ON ( R.ID = DATAA.ID )
WHEN NOT MATCHED
THEN INSERT (
ID,
AGE,
HEIGHT )
VALUES (
DATAA.ID,
DATAA.AGE,
DATAA.HEIGHT )
Cheers!!
Below is the sql query in need to run whole query together
insert into result
Select t1.Id, t1.Age, t2.Height from Table1 t1 inner join Table2 t2 on t1.Id=t2.Id where t1.status=1
i want update one column in sql on basis of id i.e. insert id values in the my new column
tb1
id1 name1
1 A
2 B
3 C
TB2
ID2 name id1 name1
1 X 1 null
2 Y 2 null
3 Z 1 null
what i wanted update TB2 name1 column on the basis of id1
Update TB2 SET TB2.name1 = tb1.name1 FROM tb1 ,TB2 WHERE tb1.id1 = TB2.id1
try this code in MS SQl
You could try update with join
update t2 set t2.nam1 = t1.name1 from tb2 t2
join tb1 t1 on t1.id1 = t2.id1
I have two tables
Table A
Number
111
222
333
444
Table B
Number Another
111 AAA
222 BBB
666 CCC
777 DDD
What I am would like to do, is apply an UPDATE statement conditional on whether the "Number" value in Table B exist in Table A. So the table would end up looking something like this.
Number Another
111 ZZZ
222 ZZZ
666 CCC
777 DDD
I know I need to use an UPDATE query and probably some kind of JOIN, but I am not sure on the syntax.
Any help greatly appreciated.
Yes. You need to update using a join like this:
update t2
set t2.Another = 'ZZZ'
from table1 t1
join table2 t2 on t1.Number = t2.Number
You can use exists also.
Query
update t1
set t1.[Another] = 'ZZZ'
from [TableB] t1
where exists(
select 1 from [TableA] t2
where t1.[Number] = t2.[Number]
);
You can use directly SELECT FROM table1 and update into table2 :
UPDATE table2 SET Another = 'ZZZ'
FROM table1 t1 WHERE t1.Number = table2.Number
I have to tables :
Table1
--------------------------------
ID VAL1 DATE1
--------------------------------
1 1 20/03/2015
2 null null
3 1 10/01/2015
4 0 12/02/2015
5 null null
Table2
--------------------------------
ID VAL2 DATE1
--------------------------------
1 N 02/06/2015
1 N 01/08/2015
2 null null
3 O 05/04/2016
3 O 02/02/2015
4 O 01/07/2015
5 O 03/02/2015
5 N 10/01/2014
5 O 12/04/2015
I want to update :
column VAL1 (of Table1) with '0', if VAL2 (of Table2) is equal to 'O'
column DATE1 (of Table1) with the earliest DATE2 (of Table2) for each ID (here my problem)
(This two tables are not so simple, it's just for illustration, they can be joined with the ID column).
Here my code :
UPDATE Table1 t1
SET t1.VAL1 = '0',
t1.DATE1 = (select min(t2.DATE2) --To take the first DATE for each ID where VAL2='O' (not working fine)
FROM Table2 t2, Table1 t1
WHERE trim(t2.ID) = trim(t1.ID)
AND VAL2='O')
WHERE EXISTS (SELECT NULL
FROM Table2 t2
WHERE trim(t2.ID) = trim(t1.ID)
AND t2.Table2 = 'O')
AND VAL1<>'0'; --(for doing the update only if VAL1 not already equal to 0)
The expected result is :
Table1
--------------------------------
ID VAL1 DATE1
--------------------------------
1 1 20/03/2015
2 null null
3 0 02/02/2015
4 0 01/07/2015
5 0 10/01/2014
The result I get is :
Table1
--------------------------------
ID VAL1 DATE1
--------------------------------
1 1 20/03/2015
2 null null
3 0 10/01/2014
4 0 10/01/2014
5 0 10/01/2014
My problem is that the DATE1 is always updated with the same date, regardless of the ID.
You shouldn't have a second reference to table1 in the first subquery; that is losing the correlation between the subquery and the outer query. If you run the subquery on its own it will always find the lowest date in table2 for any ID that has val2='O' in table1, which is 10/01/2014. (Except your sample data isn't consistent; that's actually N so won't be considered - your current and expected results don't match the data you showed, but you said it isn't real). Every row eligible to be updated runs that same subquery and gets that same value.
You need to maintain the correlation between the outer query and the subquery, so the subquery should use the outer table1 for its join, just like the second subquery already does:
UPDATE Table1 t1
SET t1.VAL1 = '0',
t1.DATE1 = (select min(t2.DATE2)
FROM Table2 t2
WHERE trim(t2.ID) = trim(t1.ID)
AND VAL2='O')
WHERE EXISTS (SELECT NULL
FROM Table2 t2
WHERE trim(t2.ID) = trim(t1.ID)
AND t2.Val2 = 'O')
AND VAL1<>'0';
You can use this UPDATE statement.
UPDATE TABLE1 T1
SET T1.VAL1 = '0',
T1.DATE1 = (SELECT MIN(T2.DATE2)
FROM TABLE2 T2
WHERE TRIM(T2.ID) = TRIM(T1.ID)
AND T2.VAL2='O')
WHERE T1.ID IN (SELECT T2.ID FROM TABLE2 T2 WHERE T2.VAL2='O')
Hope it will help you.
MYSQL Solution
Hope this MySql syntax also works with ORACLE.
The issue with the SQL is that it only consider the records with VAL2=='O' when calculating the earliest date. So the last record have the date as shown in table below. Record "5 N 10/01/2014" is not considered.
UPDATE Table1, (SELECT * FROM (SELECT * FROM table2 WHERE VAL2='O' ORDER BY ID, DATE1) X GROUP BY X.ID) T2
SET Table1.DATE1=T2.DATE1, Table1.VAL1=0
WHERE Table1.ID=T2.ID
..
Table1
--------------------------------
ID VAL1 DATE1
--------------------------------
1 1 20/03/2015
2 null null
3 0 02/02/2015
4 0 01/07/2015
5 0 **03/02/2015**
Tested on MySql 5.6.14
I have two tables. I need to take away from the first table second table. Stim to another table without all the rows of the first table.
Table1
Table2
Result = Table1(value1) – Table2(value1) -----groupe no. 2 or no.1
Result (groupe no. 2)
Result
id value1 groupe
_______________________
1 10 2
2 9 2
3 10 2
5 5 2
6 11 2
7 12 2
I need the result of which I can write the group number and get result for that group.
Try this query:
Select
t1.id,
t1.value1-t2.value1 as value,
t1.groupe from
table1 t1,table2 t2
where t1.id=t2.id and t1.groupe=2;
try this:
SELECT
T1.id, T2.group, T1.valor - T2.valor AS value
FROM
Table1 T1 INNER JOIN
Table2 T2 ON T1.id = T2.id AND T1.group = T2.group
WHERE (T1.group = 2)