I have following two tables (Tab1 and Tab2) where I needs to check for mismatch values of T1Col3 and T2Col3.
Link columns of those two tables are T1Col1, T1Col2 with T2Col2, T2Col2
But When I run the following query It doesn't give exact records that I want
SELECT * FROM Tab1 INNER JOIN Tab2
ON T1Col1 = T2Col1
AND T1Col2 = T2Col2
WHERE T1Col3 <> T2Col3
Sample data
Tab1
T1Col1 T1Col2 T1Col3
jkl nnn qoq
efg Aaa wow
efg xxx yoy
abc iii ror
abc fff uou
abc rrr rr3
gdg ppp 123
abc www 234
jkl qqq 336
jkl nnn 888
Tab2
T2Col1 T2Col2 T2Col3
jkl nnn qoq
efg aaa wow
efg xxx yoy
abc fff uou
ABC iii ror1
abc rrr kok
gdg ppp 789
jkl nnn utu
jkl qqq 336
Result
T1Col1 T1Col2 T1Col3 T2Col1 T2Col2 T2Col3
jkl nnn 888 jkl nnn qoq
abc iii ror abc iii ror1
abc rrr rr3 abc rrr kok
gdg ppp 123 gdg ppp 789
jkl nnn 888 jkl nnn utu
jkl nnn qoq jkl nnn utu
Unwanted records of above results
jkl nnn 888 jkl nnn qoq
jkl nnn qoq jkl nnn utu
How can I get rid of highlighted records. I think this happens due to matching duplicate records
Cheers
Shabar
You just need third and in inner join if you want to remove mismatched third column:
SELECT * FROM #Tab1 INNER JOIN #Tab2
ON T1Col1 = T2Col1
AND T1Col2 = T2Col2
AND T1Col3 = T2Col3
Update:
Select * From
(
Select * From tab1
Except
Select * From tab2
)tbl
Inner Join
(
Select * From tab2
Except
Select * From tab1
)t2
On tbl.T1Col1=t2.T2Col1
And tbl.T1Col2=t2.T2Col2
For MS Access
Select * From
(
Select t1.* From Tab1 t1
Left Join Tab2 On t1.T1Col1=T2Col1 And t1.T1Col2=T2Col2 And t1.T1Col3=T2Col3
Where T2Col1 Is Null And T2Col2 Is Null And T2Col3 Is Null
)tbl
Inner Join
(
Select t2.* From Tab2 t2
Left Join Tab1 t1 On t1.T1Col1=t2.T2Col1 And t1.T1Col2=t2.T2Col2 And t1.T1Col3=t2.T2Col3
Where t1.T1Col1 Is Null And t1.T1Col2 Is Null And t1.T1Col3 Is Null
)t2
On tbl.T1Col1=t2.T2Col1
And tbl.T1Col2=t2.T2Col2
Related
I have following three tables, from which I would like to get a specific result
TableA
key1 key2
-------------
121 4
131 4
141 5
151 3
161 3
171 6
181 6
191 6
... ...
TableB:
key1 key3
-------------
121 1001
131 1111
141 1111
151 1222
161 1222
171 1234
181 1001
191 1111
... ...
TableC:
key3 key4
-------------
1001 "aa"
1111 "gg"
1222 "hh"
1234 "jj"
... ...
I want a SQL query (which could use inner join) to give me the following result:
key2 key4
-------------------------
3 "hh"
4 "aa", "gg"
5 "gg"
6 "aa", "gg", "jj"
Microsoft SQL Server 2012
You can use string_agg():
select t1.key2, string_agg(t3.key4, ',') key4
from table1 t1
inner join table2 t2 on t2.key1 = t1.key1
inner join table3 t3 on t3.key3 = t1.key3
group by t1.key2
Please try with below SQL query:
SELECT key2, string_agg(key4, ",")
FROM TableA JOIN TableB USING (key1) JOIN TableC USING (key3)
GROUP BY key2
I want to join few tables:
table1:
A B_key B_version C D
123 abc 1 ccc 11
123 abc 2 ddd 11
456 dfg 1 rrr 22
789 vvv 1 55
table2:
A E F
123 s 5
456 r
111 t 2
table3:
B_key B_version G
abc 1 aa
abc 1 bb
abc 2 aa
abc 2 cc
dfg 1 aa
so the result would look like this:
A B_key B_version C D E F G
123 abc 1 ccc 11 s 5 aa
123 abc 1 ccc 11 s 5 bb
123 abc 2 ddd 11 s 5 aa
123 abc 2 ddd 11 s 5 cc
456 dfg 1 rrr 22 r aa
789 vvv 1 55
Version can go as high as 8.
IF I don't have A, B_key or B_version - the line is useless. Otherwise I need to keep all the information I do have.
In reality I have many more columns.
I've tried:
SELECT table1.A, table 1.B_key, table 1.B_version, table 1.C, table 1.D,
table2.E, table2.F,
table3.G
FROM table1
LEFT JOIN table2
ON table1.A = table2.A
LEFT JOIN table3
ON table1.B_key = table3.B_key AND
table1.B_version = table3.B_version
and the same with FULL JOIN.
It ends up the same: for every B_key only the highest B_version is kept, while the others disappear.
How can I avoid loosing information?
You can use left joins among tables as below :
select t1.A, t1.B_key, t1.B_version, t1.C, t1.D, t2.E, t2.F, t3.G
from table1 t1
left join table2 t2 on t2.A = t1.A
left join table3 t3 on t3.B_key = t1.B_key and t3.B_version = t1.B_version
Demo
in order to bring also the rows for unmatched values for join conditions.
If I understand correctly, you want all the b_keys and b_versions from table1 and table3. Then you want to bring in the other data. That suggests left joins
select . . .
from ((select B_key, B_version
from table1
) union -- on purpose to remove duplicates
( select B_key, B_version
from table3
)
) bb left join
table1 t1
on t1.b_key = bb.b_key and
t1.b_version = bb.b_version left join
table2 t2
on t2.a = t1.a left join
table3 t3
on t1.b_key = bb.b_key and
t1.b_version = bb.b_version;
TableA
------
IntId EvId Name Phone1
===== ==== ==== ======
100 aaa xxx 11111
101 bbb yyy 22222
102 ccc zzz 33333
103 bbb asd 44444
104 bbb sdf 55555
TableB
------
IntId ASId Grp Phone2
===== ==== ==== ======
201 bbb yyy 6666
202 ccc zzz 7777
203 bbb asd 8888
204 bbb kkf 9999
205 ddd esd 0000
206 eee ffr 1001
I want to join TableA with TableB using TableA.EvId = TableB.ASId to output {IndId, EvId, Name, Phone1, Grp, Phone2} (using outerjoin as I want all records in TableA with matching TableB columns)
I could do it using below query, but it is giving me duplicates since EvId and ASId have duplicates.
SELECT a.IntId, a.EvId, a.Name, a.Phone1, b.Grp, b.Phone2
FROM TableA a
LEFT OUTER JOIN TableB b
ON a.EvId = b.ASId
If EvId are duplicates, consider that record which has max IntId. Same rule for TableB
My final output should be like this - all unique EvId's in TableA using outerjoin on B
IntId EvId Name Phone1 Phone2 Grp
100 aaa xxx 11111 null null
104 bbb sdf 55555 9999 kkf
102 ccc zzz 33333 7777 zzz
Can you please help me with the query?
You can use ROW_NUMBER():
SELECT a.IntId, a.EvId, a.Name, a.Phone1, b.Grp, b.Phone2
FROM TableA a LEFT OUTER JOIN
(SELECT b.*,
ROW_NUMBER() OVER (PARTITION BY ASId ORDER BY IntID DESC) as seqnum
FROM TableB b
) b
ON a.EvId = b.ASId AND b.seqnum = 1;
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;
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?