use the redundant values in SQL - sql

I have the following issue on SQL.
What I have (table A and table B)
What I want
My aim is to have a third table with the name of the file.
Info: my tables do not have any keys.
I've tried the code but it is not doing what I want
create table correspondance as
select b.var1, a.var1, a.var2
from A a
inner join B b on a.var1 = b.var2;
Any insights are welcomed.
Edited
I have this result :
var1 var2
azdt.csv fich11.csv
zlata.csv fich13.csv

You're just joining on the wrong fields, and too many fields selected
select a.var1, b.var2
from A a
inner join B b on a.var2 = b.var1

Related

SQL Logic: When joining Child table B to Parent Table A on A.FID = B.ID

I have been wondering if the results would change in multi-join tables queries.
If you have parent Table A
A B
ID|FID FID
1|2 1
2|4 2
3|5 3
4|7 4
5|8 5
6|NULL 6
7|NULL 7
8|NULL 8
does it matter which table column you specified in the WHERE clause?
For example, what is the difference between the two:
Select *
From Table A
Left Join B on A.FID = B.FID
WHERE A.FID IN (2,5,8)
Select *
From Table A
Left Join B on A.FID = B.FID
WHERE B.ID IN (2,5,8)
Thank you for the help!
EDIT:
Micheal has solved my question and I have tested it out
'Actually, while your answer is a good one (and probably the one he's looking for), since both of his queries are essentially filtering on the primary key of B (A.FID, B.ID), they actually are logically identical (assuming that A.FID is a true foreign key constraint on B). That is, both queries filter out rows in which B.ID is not 2, 5 or 8.' – Michael L.
It is only different is Table B is the main table and you queried based on B.ID as in:
SELECT *
FROM B
LEFT JOIN A ON A.FID = B.FID
WHERE B.FID IN (2,5,8)
While this will be the same as having A as the main table:
SELECT *
FROM B
LEFT JOIN A ON A.FID = B.FID
WHERE A.FID IN (2,5,8)
Yes, it does.
When you use an OUTER JOIN, values from one of the tables may be NULL. So, the second query is equivalent to:
Select *
From Table A Inner Join
B
on A.FID = B.ID
WHERE B.ID IN (2, 5, 8);
because the NULL values are filtered out.
As a general rules with LEFT JOIN:
Filters on the first table belong in the WHERE clause.
Filters on the second and subsequent tables should to in the ON clause.

Omitting a list of observations in sas sql

I'm trying to create a table in sas that will take observations from a specific column as long as they aren't list in another column in another table.
I've used the code:
proc sql;
create table tbl as
select a.var1, a.var2, a.var3 from
tblA as a, tblB as b
where a.var1~=b.var1;
quit;
Would it be because I've assigned b as a table I'm not selecting a variable from? or is my condition just incorrect?
Your condition is incorrect, you need to tell the tables how to join (where the equality is) THEN tell them that you only want those that dont match.
A left join is used for this:
select a.var1, a.var2, a.var3
from
tblA as a
left join tblB as b on a.var1 = b.var1
where
b.var1 is null
Where a are the values you want that dont match b.
See SAS SQL join examples for more
This can also be accomplished using NOT IN

Separated JOIN form main INNER JOINS's

I want to INNER JOIN some tables and then insert a condition where the entries of a table are dependant on another table (that was not joined with the others)
Something like this:
SELECT * FROM TABLE_A AS a
INNER JOIN TABLE_B AS b ON b.id_b=a.id_a
INNER JOIN TABLE_C AS c ON c.id_c=b.id_b
Now I want to add a condition (possibly a "WHERE" clause) that only selects the values in a field in TABLE_C that match another condition, the existence of a value in a field in TABLE_D
Possible statement:
WHERE c.code=d.another_code AND d.reg_number LIKE 999%
How do i declare in the query the TABLE_D, since I do not want to Join it with the others?
In other words, I want to intersect 3 sets (A,B,C) and the other one (set D) is intersected only with set C
The title of the question Run-time error '13': ... doesn't seem to match the content so I'll just answer the SQL part.
Maybe this is what you want?
SELECT * FROM TABLE_A AS a
INNER JOIN TABLE_B AS b ON b.id_b=a.id_a
INNER JOIN TABLE_C AS c ON c.id_c=b.id_b
WHERE c.code = -- or possiby IN instead of =
(SELECT another_code FROM TABLE_D WHERE another_code LIKE '999%')
If the subquery can return multiple rows you need to use WHERE c.code IN instead of WHERE c.code =

Can this be done with a single SQL Join?

I am not sure if this can be done with a single JOIN, but I basically have two tables with an ID column in common. To make it simple I'll say Table A just contains an ID while Table B contains an ID and Code. There is a 1:M relationship between Table A and Table B, however it's also possible an ID from Table A is not contained in Table B at all. I was hoping to have a query return every ID that exists in Table B within a particular code range, or does not exist in Table B at all.
I tried using a LEFT JOIN with something like:
SELECT A.id FROM A LEFT JOIN B ON A.id = B.id AND b.code BETWEEN '000' AND '123'
But, this still gives me the IDs that exist in Table B outside of the code range.
Use a left join, and filter the result to contain the codes in the range, and also the lines where there is no matching record in table B:
select
A.id
from
A
left join B on B.id = A.id
where
B.code between '000' and '123' or B.id is null
What about
SELECT id FROM A LEFT JOIN B ON A.id = B.id
WHERE b.code IS NULL OR b.code BETWEEN ' ' AND '123'

When exactly will the result of a SQL join collapse two columns into one?

I use PostgreSQL quite frequently, and I've observed that some equality joins merge the two columns involved in the equality comparison into a single column in the result set, and some don't. It seems to be related to whether or not the join is INNER or OUTER, and also whether or not the join condition is expressing with the USING syntax or ON syntax. I'd like to know when exactly the two columns are collapsed/merged into one, or even better: I'd like to know where this behavior is specified.
It will be collapsed when using is used
create table a (id int);
create table b (id int);
select *
from
a
inner join
b using (id);
id
----
(0 rows)
If on is used it will return two columns:
select *
from
a
inner join
b on a.id = b.id;
id | id
----+----
(0 rows)
And the column without a table name qualification is ambiguous
select id
from
a
inner join
b on a.id = b.id;
ERROR: column reference "id" is ambiguous
LINE 1: select id from a inner join b on a.id = b.id;
The same for outer join
select *
from
a
full outer join
b on a.id = b.id;
id | id
----+----
(0 rows)