Merging to table from 2 joined tables - sql

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

Related

UPSERT with SELECT in HANA

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);

Joining every row of table 1 to table 2

I have two tables identical to each other like below
table 1
col1
1
2
3
4
5
table 2
col1
1
2
3
4
5
is there way to write a SQL query to join every row of table 1 to every row of table 2?
Do you want a Cartesian product? If so, use cross join:
select t1.col1, t2.col2
from table1 t1 cross join
table2 t2;
It sounds like an inner join as they are identical tables:
select t1.col, t2.col, ...
from table t1
inner join t2 on t1.col = t2.col

SQL query : Inner join with distinct values

I have two tables :
Table 1
ID | Name
12 User1
77 CostCenter1
78 CostCenter2
79 CostCenter3
14 User2
Table 2
UserID | AssignedCostCenter
12 77
12 78
12 79
14 78
1st table collects identities (users, cost centers).
2nd table represents users and their allowed CostCenters.
I would like to achieve the following:
query should returns a single column which have a list of (unique) users who have assigned more then 1 cost center (two and more).
I started with join two tables by:
Select Table 1.Name
from Table 1
inner join Table 1.ID = Table 2.UserID
The result is:
User1
User1
User1
User2
You are getting "user1" and "user2" because of inner join based upon Ids.
To get the list of users who have been assigned more than 1 cost center (two and more), modify your query as:
SELECT Table 1.Name
FROM Table 1
INNER JOIN Table 2 ON Table 1.ID = Table 2.UserID
GROUP BY Table 1.Name
HAVING count(Table 1.name) > 1
Using group by on the userid in table2 allows use of a having clause which filters out those who only have 1 cost centre, then the inner join to that result will list the just the names of those who have more than one cost centre.
SELECT
Table1.Name
FROM Table1
INNER JOIN (
SELECT
UserID
FROM Table2
GROUP BY
UserID
HAVING COUNT(UserID) > 1
) AS t2 ON Table1.ID = t2.UserID
Your question is unclear on whether table 2 can have duplicates. So, it might be safer to approach this query as:
SELECT t1.Name
FROM Table1 t1 JOIN
Table2 t2
ON t1.ID = t2.UserID
GROUP BY t1.Name
HAVING MIN(t2.AssignedCostCenter) <> MAX(t2. AssignedCostCenter);

sql with two table and group and result from that table?

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)

SQL Access help in sum from 2 different tables

i have these tables
table 1
id price
1 30
2 40
3 50
table 2
id price
1 70
2 5
3 10
i want a query that would sum the the price value based on the ID
like if table1.id=table2.id then sum table1.price and table2.price
the end result should be something like this
table 3
id price
1 100
2 45
3 60
You can;
SELECT
TABLE1.ID,
TABLE1.PRICE+TABLE2.PRICE
FROM TABLE1
INNER JOIN TABLE2 ON TABLE1.ID = TABLE2.ID;
Or if there are duplicate IDs in either table;
SELECT
TABLE1.ID,
SUM(TABLE1.PRICE+TABLE2.PRICE)
FROM TABLE1
INNER JOIN TABLE2 ON TABLE1.ID = TABLE2.ID
GROUP BY TABLE1.ID;