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;
Related
I try co create a SQL query with two inner joins and an if case. I create an example to explain what I mean:
ID
Typ
Case
123
AAA
zzz
124
BBB
yyy
125
CCC
yyy
Typ1
ID1
AAA
888
BBB
999
CCC
777
ID2
Result
666
1
555
2
777
3
In words, the query should do:
Search in the first table for ID 125, so I get Typ CCC and Case yyy
If case is yyy then search in the second table for CCC in column Typ1, here I get the ID 777 and then search in the third table for 777 in column ID2 to get the result 3.
If case is not yyy then just show me the results of the first table.
The result should be:
ID
Typ
Result
123
AAA
No match
124
BBB
No match
125
CCC
3
I hope you can understand what I try to explain :)
You want to select data from the first table and only show data from the other tables where appropriate. So, outer join the other tables.
select t1.id, t1.typ, t3.result
from t1
left outer join t2 on t2.typ1 = t1.typ and t1.case = 'yyy'
left outer join t3 on t3.id2 = t2.id1
order by t1.id;
I have two tables Table1 and Table2 with some records
How to select column unmatched and missing records from two table in sql server
Table1
id name place
1 AAAA bangalore
2 BBBB IDLE
3 CCCC Chennai
4 DDDD NOT Reachable
Table 2
id name dept place
1 AAAA IT bangalore
2 BBBB Sales mumbai
3 CCCC Support Chennai
4 DDDD IT Delhi
5 MMMM Software Mumbai
I want the result like this
id name
2 BBBB
4 DDDD
5 MMMM
i have got unmatched rows using below. how can I get unmatched and missing one in single select query?
select t2.name[place from table2]
from tab1 t1 inner join tab2 t2
on t1.id = t2.id and t1.place <> t2.place
Hmmm . . . If I understand correctly, you want everything from table2 that doesn't have a corresponding match in table1. For that, you can use not exists:
select t2.*
from table2 t2
where not exists (select 1
from table1 t1
where t1.id = t2.id and t1.name = t2.name and
t1.place = t2.place
);
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.
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
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