how to change the column name dynamically? - sql

In a database there are two tables.
Table1 and Table2
Table1:
column1 column2 column3
271 211 111
301 333 333
Table2:
ColumnNo Value Desc
1 271 aaa
3 111 bbb
2 211 ccc
2 333 ddd
1 301 eee
Here columnNo refers to the 1st table>> 1=Column1,2=column2,3=Column3.
so i have to update the 1st table column value with the Desc.
so the updated table1 will be
column1 column2 column3
aaa ccc bbb
eee ddd ddd
like this.
As there are lots of column and value so how can i change the column name dynamically and update the value?

Try this
UPDATE T1
SET T1.column1 = T2.Desc,T1.column2 = T3.Desc,T1.column3 = T4.Desc
FROM Table1 T1 Inner Join Table2 T2 ON T1.column1 = T2.Value
Inner Join Table2 T3 ON T1.column2 = T3.Value
Inner Join Table2 T4 ON T1.column3 = T4.Value
FIDDLE DEMO

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

How can I find duplicate records \ values after left join?

I have:
TABLE1 with ID, and Name.
TABLE2 with ID, and Address.
I want to get ALL TABLE1 records and add a STATUS column:
If this record exists in TABLE2 - 'OK'.
If this record is not exist in TABLE2, then research a match by last 2 digits, else 'NO_RECORD'.
If this record has duplicate records in TABLE2, then if the duplicate records has the same Address choose one record - 'OK', and if they doesn't has the same Address - 'DUPLICATE'.
Meanwhile, I've started with this:
SELECT t1.id,
t1.name,
t2.Address,
iif(Address is null, 'No_RECORD', 'Ok') as 'status'
FROM Table1 as t1
left join Table2 as t2 on t1.id = t2.id
For example:
Table1
id Name
111 aaa
222 bbb
333 ccc
444 ddd
555 eee
666 fff
777 ggg
888 hhh
999 iii
Table2:
id Address
111 rr
922 hfh
444 vbv
444 vbv
555 xxa
555 plo
555 plo
666 wqq
777 gyt
999 ree
999 ree
My accepted results are:
id name Address 'status'
111 aaa rr Ok
222 bbb hfh Ok
333 ccc No_RECORD
444 ddd vbv Ok
555 eee Duplicate
666 fff fff Ok
777 ggg wqq Ok
888 hhh No_RECORD
999 iii ree Ok
444 is not duplicate because the 2 table2's records address are match,
555 is duplicate because the 3 table2's records address are mismatch,
999 is not duplicate because the 2 table2's records address are match.
222 is ok because tha 2 last digits of it's id are exist in tabl2: "922".
How can I continue? (I use sql query in access).
I have no idea what "match by last five digits" is supposed to mean. It has nothing to do with your sample data, so I'm just ignoring that part of the question.
What you want to do is to aggregate on table2 before doing the join:
select t1.id, t1.name,
iif(multiple_addresses = 0, address, null) as address,
switch(t2.id is null, "No_Record",
multiple_addresses = 1, "Duplicate",
1=1, "OK"
) as status
from table1 as t1 left join
(select id, min(address) as address
iif(min(address) = max(address), min(address), 0, 1) as multiple_addresses
from table2
group by id
) as t2
on t1.id = t2.id;
You can use a case statement to get this status:
SELECT
t1.id,
t1.name,
t2.Address as add2,
case
when t2.Address is null and exists (select 1 from Table2 temp where temp.id = t1.id) then 'DUPLICATE'
when t2.Address is null then 'NO_RECORD'
else 'OK' end
as 'status'
FROM Table1 as t1
left join Table2 as t2 on t1.id = t2.id and t1.name = t2.Address
order by t1.id
As you can see I added and t1.name = t2.Address to the join clause to make sure you have non null Table2 values only when you want so.
About the case when, the first condition checks if 1) no corresponding record was found 2) there are records for the same id, meaning 'DUPLICATE'. The second condition checks that there are no corresponding record, and we already know there are no duplicates (as it would have fallen into the first case).
Working SQLFiddle.

UPDATE based on if value exist in another table

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

Column comparison SQL issue in MS Access

I have the following query and needs to get the mismatch column records in MS Access.
The issue is when there are more than one records with same link column value, matching is not happening properly
Query
SELECT
T1Col1,
T1Col2,
T2Col1,
T2Col2
FROM T1
INNER JOIN T2
ON T1.Col1 = T2.Col1
WHERE T1.Col2 <> T2.Col2
After executing I am getting below kind of results which is not correct
T1Col1 T1Col2 T2Col1 T2Col2
abc ccc abc eee
abc eee abc ccc
Ideally above records should not return in the result set as those matches (checking for not matching ones). Do I need to change anything in the query to get the correct results
T1
T1Col1 T1Col2
jkl ttt
efg qqq
efg mmm
abc ccc
abc eee
T2
T2Col1 T2Col2
jkl sss
efg uuu
efg mmm
abc eee
abc ccc
Expected results would be
T1Col1 T1Col2 T2Col1 T2Col2
jkl ttt jkl sss
efg qqq efg uuu
I doubt this is the most efficient solution, but it'll work.
The problem with a simple join is that you are performing a cartesian join of ALL records in T1 with ALL records in T2, then deleting the ones that fail your criteria. Thus, you get results you're not asking for, because you're not doing the query that matches your english (or native-language-of-your-choice). SQL is great that way - most of the time, if you just say out loud what you want, exactly, the SQL matches.
What you want:
Select those rows from T1 where there is no row in T2 with the same
value for COL1 and COL2. Then select those rows from T2 where there
is no row in T1 with the same value for COL1 and COL2. Then match
these two result sets up to each other by COL1.
So, here you go:
create table t3 as
select * from
(select * from t1 where not exists (select 1 from T2 where T1col1=t2col1 and t1col2=t2col2)) M1,
(select * from t2 where not exists (select 1 from T1 where T1col1=t2col1 and t1col2=t2col2)) M2
where M1.t1col1=M2.t2col1;

Query to get ALL matches for a particular column

Suppose I have two tables Table1 and Table2 with the following data.
Column1 Column2 Column3
AAA KKK 9
BBB LLL 7
CCC MMM 9
DDD MMM 5
EEE MMM 7
FFF NNN 9
GGG OOO 1
Column4 Column1
TTT DDD
TTT BBB
UUU EEE
VVV BBB
WWW AAA
WWW BBB
XXX DDD
YYY EEE
YYY DDD
YYY CCC
YYY FFF
The query is of selecting "select value(s) from column4 which matches the tuple result of column1 when column2 has the value 'MMM'('CCC','DDD','EEE') this result should match with all results from column4"
the result is 'YYY'
The error message is
SELECT DISTINCT t2.Column4
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.Column1 = t2.Column1
WHERE Column2 = 'MMM'
GROUP BY t1.Column2
HAVING COUNT(t1.Column1) = COUNT(t2.Column1)
*
ERROR at line 1:
ORA-00904: "T1"."Column1": invalid identifier
SELECT t2.Column4
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.Column1 = t2.Column1
WHERE Column2 = 'MMM'
GROUP BY t2.Column4
HAVING COUNT(t1.Column1) = COUNT(t2.Column4)
What about this query?