I have two tables, A1 and A2. I want to compare these two tables. I tried inner join but it doesn't give the required result.
These are the data in these tables,
Table A1
No. Address
1 abc
1 abc
1 def
1 def
Table A2
No. Address
1 def
1 abc
1 abc
1 def
These two tables can only be joined by using No. column. So if I use INNER JOIN it gives 16 rows. I don't want that, I want only 4 rows to be displayed. This should be the output:
No. Address eq
1 abc #
1 abc *
1 abc #
1 abc #
Last column is displayed if address in A1 is equal to A2
Search for records that exist in A1 table but not in A2 table:
SELECT * FROM A1 WHERE NOT EXISTS (SELECT * FROM A2 WHERE A2.Id = A1.Id)
try to use case
select case when tablea1.address=tablea2.address then '*'
else '#' end as eq from tablea1 inner join tablea2 on tablea1.=tablea2.
hope it helps you.
Related
I have 2 large tables like bellow in Teradata. I need to join them so that:
all records from Table A are kept - like A left join B
join is on A.client_id=B.client_id_1
but if B.client_id_1 is null, it can join on A.client_id=B.client_id_2
Table A
client_id
details_a
1
abc
2
def
3
ghi
4
jkl
Table B
client_id_1
client_id_2
details_b
1
null
123
null
2
456
3
3
789
The result should be like:
client_id
details_a
client_id_1
client_id_2
details_b
1
abc
1
null
123
2
def
null
2
456
3
ghi
3
3
789
4
jkl
null
null
null
The tables are large and the join is part of a larger script (other joins using Table B)
I tried something like
Table A LEFT JOIN Table B
ON (A.client_id = B.client_id_1 OR A.client_id = B.client_id_2)
But the result was product join that never finished.
I also want to avoid two left joins (on B.client_id_1 and on B.client_id_2) as it would result in having all columns from Table B twice. And the Table B is further used in following joins. Plus client_id=3 would have two records.
Any idea? And what is wrong with the JOIN using OR above?
Thanks, R.
You can use case statement:
Table A LEFT JOIN Table B
ON (A.client_id = case when B.client_id_1 is null then B.client_id_2 else B.client_id_1 end)
oR Coalesce:
Table A LEFT JOIN Table B
ON (A.client_id = Coalesce(B.client_id_1 ,B.client_id_2 ))
If B.client_id_1 is not null then Coalesce(B.client_id_1 ,B.client_id_2 ) will return B.client_id_1 but if it's null then the condition will return B.client_id_2 .
I have a table A and B, both has one column and two rows as follows,
A B
-------
C1 C2
1 1
1 1
1 0
0 0
If I apply a inner join on this and the it is returning 8 rows as a results,
Select C1,C2 from A inner join B on A.C1=B.C2;
Result
---------
C1 C2
1 1
1 1
1 1
1 1
1 1
1 1
0 0
0 0
And I am guessing that, first row of the C1 column is checking with the data of all C2 columns. If it's matches, its returning result otherwise, it won't. The same method was following for the rest of the columns. Correct me, if my understanding was wrong and please assist with the answer for the below question;
I have two tables A,B both are having two columns. What will be the result if we apply inner join, please explain me with functionality.
A B
----–-----------
C1 C2 C3 C4
1 1 1 1
1 1 1 0
Select C1,C2,C3,C4 from A inner join B on A.C1=B.C3;
It's returning 4 rows, please explain how?
This is common misconception about inner joins. Concept of inner join says a value in a column of a table will match with each and every occurrence of same value of joining column in another table.
In your example, in table A First row of 1 of column C1 will match with all 2 rows of value 1 of column C2 of table B, Second 1 will match with all 2 1's then 3rd 1 will match with all 2 of able B. Then a 0 will match 2 times in table B.
Thus they becomes - 2(1's) + 2(1's) + 2(1's) + 2(0's) = 8 rows.
Same concept applies to your second example as well. Since you have 2 columns in your 2nd example, So you have to decide the join predicate here.
If you decided to join like `A.C1 = B.C3` then 4 rows will occur in result.
If you decided to join like `A.C1 = B.C4` then 2 rows will occur in result.
If you decided to join like `A.C2 = B.C3` then 4 rows will occur in result.
If you decided to join like `A.C2 = B.C4` then 2 rows will occur in result.
In your example, if you use the predicate A.C1 = B.C3 the result is:
c1 c2 c3 c4
--- --- --- --
1 1 1 1
1 1 1 1
1 1 1 0
1 1 1 0
See running example at DB Fiddle.
Now, as a general rule, the inner join will match rows from both tables according to any predicate you specify, not necessarily simple column values.
For example:
A B
-------- --------
C1 C2 # C3 C4 #
1 1 A1 1 1 B1
1 1 A2 1 0 B2
0 1 B3
If the predicate is a.c1 * a.c2 = b.c3 + b.c4, as in the query:
select
a.*,
b.*
from a
join b on a.c1 * a.c2 = b.c3 + b.c4
The result is:
c1 c2 c3 c4 matching predicate
--- --- --- -- --------------------------
1 1 1 0 1 * 1 = 1 + 0 (A1 and B2)
1 1 1 0 1 * 1 = 1 + 0 (A2 and B2)
1 1 0 1 1 * 1 = 0 + 1 (A1 and B3)
1 1 0 1 1 * 1 = 0 + 1 (A2 and B3)
Do you see how the rows are matched?
I have two tables in SQLITE
"port" is
ticker weight
abc 1
bcd 2
cde 3
"bench2" is
ticker weight
abc 3
bcd 2
cde 1
and Im trying to learn how to use an outer join in SQLITE. Starting with a LEFT OUTER JOIN as follows
SELECT * FROM port LEFT OUTER JOIN bench2 ON port.ticker = bench2.ticker;
I get the result
ticker weight ticker weight
abc 3 NULL NULL
bcd 2 bcd 2
when I'd expect to see
ticker weight weight
abc 1 3
bcd 2 2
cde 3 1
what am I doing wrong and how would I get the result I want?
Second question if possible - how could I get an additional row of
ticker weight weight
abc 1 3
bcd 2 2
cde 3 1
def NULL 3
in the result table if "bench2" had an extra row,
ticker weight
abc 3
bcd 2
cde 1
def 3
while port remained the same.
Reverse-engineering your result leads me to conclude that your data are different than you say. You appear to have this:
port:
ticker weight
abc 3
bcd 2
... (no other rows)
bench2:
ticker weight
bcd 2
... maybe other rows, but none having ticker = 'abc'
As for the columns, when you perform any join other than a NATURAL JOIN, there is one result column for each column in each of the joined tables, including when there are columns with the same name, and including columns appearing in the join condition. You can limit which of those columns appear in the final query result, and / or you can assign preferred aliases to them, by specifying in the select list which ones you want.
How about something like this?
SELECT table2.ticker,
table1.weight,
table2.weight
FROM table2
LEFT OUTER JOIN table1
ON table1.ticker = table2.ticker
Hello I need some help regarding SQL Query.
Table A
ID Name Type
---------------------
1 abc BC
---------------------
2 def SD
---------------------
3 ghi BC
----------------------
Table B (BC_ID and SD_ID are Foreign keys of Table 'B' referencing the Primary key 'ID' of Table 'A')
ID BC_ID SD_ID
---------------------
1 1 2
---------------------
2 3 2
---------------------
I am using VB.Net datagrid view to print the Table B details. I need to print 2 more columns in datagridview which specifies the Name of the corresponding BC_ID and SD_ID.
How do I write a SQL query which can get the 'Name' of the Corresponding ID's from the database and print in datagridview...
Expected Output in datagridview:
ID BC_Name SD_Name
---------------------
1 abc def
---------------------
2 ghi def
---------------------
I dont want to have separate BC_Name and SD_Name columns in the table B..I just need to join the two tables and get the name in a single query..Is it possible? Thanks in advance for your help
You need to join TableA twice with different alias names.
Try this:
SELECT B.ID,ISNULL(A1.NAME,'') as BC_NAME,ISNULL(A2.NAME,'') as SD_NAME
FROM TableB B LEFT JOIN
TableA A1 ON A1.ID=B.BC_ID LEFT JOIN
TableA A2 ON A2.ID=B.SD_ID
Result:
ID BC_NAME SD_NAME
1 abc def
2 ghi def
See result in SQL Fiddle.
in tsql how can I turn this sample table:
ID FIELDNO ROWNO VALUE
ABC 2 1 Cat1Val1
ABC 2 2 Cat1Val2
ABC 2 3 Cat1Val3
ABC 3 1 Cat2Val1
ABC 3 2 Cat2Val2
ABC 5 1 Cat3Val1
to a table that will create three columns based on the fieldno and duplicate the rows so that it lists all possible variations of whatever fieldno has the highest rowNo?
So fieldno 2 will become CAT1, 3 -> CAT2 and 5 -> CAT3
Expected result:
ID CAT1 CAT2 CAT3
ABC Cat1Val1 Cat2Val1 Cat3Val1
ABC Cat1Val1 Cat2Val2 Cat3Val1
ABC Cat1Val2 Cat2Val1 Cat3Val1
ABC Cat1Val2 Cat2Val2 Cat3Val1
ABC Cat1Val3 Cat2Val1 Cat3Val1
ABC Cat1Val3 Cat2Val2 Cat3Val1
I could then use this as a base to join with other tables.
Here's a fiddle with more data.
I've tried to create some CASE WHEN clauses but I think this is not going to work.
Hope you can show me a way how this can be solved.
Thank you.
This seems a bit unorthodox, but this should do it for you if I understood the problem correctly:
SELECT d1.id, d1.value Cat1, d2.value Cat2, d3.value Cat3
FROM Docs d1
INNER JOIN Docs d2 ON d2.id = d1.id AND d2.rowNo = d1.rowNo AND d2.fieldNo = 3
INNER JOIN Docs d3 ON d3.id = d2.id AND d3.rowNo = d1.rowNo AND d3.fieldNo = 5
AND d1.fieldNo = 2
This solution of course expects values will exist for each column.
Revised answer...
If the third join and only the third join (Docs.fieldNo = 5) is optional, you can do something like this:
SELECT
d2.id,
d2.value Cat1,
d3.value Cat2,
d5.value Cat3
FROM
(SELECT 2 fieldNo2, 3 fieldNo3, 5 fieldNo5) f
INNER JOIN Docs d2 ON d2.fieldNo = f.fieldNo2
INNER JOIN Docs d3 ON d3.fieldNo = f.fieldNo3 and d3.rowNo = d2.rowNo and d3.id = d2.id
LEFT JOIN Docs d5 ON d5.fieldNo = f.fieldNo5 and d5.rowNo = d2.rowNo and d5.id = d2.id
I've revised the rest of the query so that hopefully what it's doing is a little clearer.
Here are some answers on joins which you may find helpful: What is the difference between "INNER JOIN" and "OUTER JOIN"?
I have tried this in an access database:
replace myvalue with value (value is a reserved word in access)
I called the table test so replace it with your tablename.
SELECT test.id, test.myValue AS cat1, test_1.[myValue] AS cat2, test_2.myValue AS cat3
FROM test, test AS test_1, test AS test_2
WHERE (((test.myValue) Like "cat1*")
AND ((test_1.[myValue]) Like "cat2*")
AND ((test_2.myValue) Like "cat3*"))
ORDER BY test.myValue, test_1.[myValue], test_2.myValue;