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?
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:
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.
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
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
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;