How to join same table twice on Access - sql

I know on MySQL we can join the same table twice by giving each table its own alias, but that doesn't seem to be working on Access.
For example:
SELECT d.departmentID, d.depName, d.location, c1.memberID, c1.fullName, c1.reportsTo, c2.fullName
FROM Departments as d
INNER JOIN Contacts as c1
ON c1.departmentID = d.departmentID
INNER JOIN Contacts as c2
ON c1.reprtsTo = c2.memberID
Doing that gives me a syntax error. Does anyone know how I can join the same table (Contacts) to get the name of the person the member reports to (c2.fullName)?
Update, the Error I'm getting:
Syntax error (missing operator) in query expression 'c1.departmentID = d.departmentID INNER JOIN Contacts as c2 ON c1.reportsTo = c2.memberI'.

In MS Access, more than one JOIN requires parentheses pairings:
SELECT d.departmentID, d.depName, d.location, c1.memberID,
c1.fullName, c1.reportsTo, c2.fullName
FROM (Contacts as c1
INNER JOIN Departments as d
ON c1.departmentID = d.departmentID)
INNER JOIN Contacts as c2
ON c1.reprtsTo = c2.memberID

In such cases it's easier to let the Access design editor take care of the joins and the aliases.
The code below is based on your code but created by the design editor:
SELECT
Departments.departmentID, Departments.depName, Departments.location,
Contacts.memberID, Contacts.fullName, Contacts_1.reportsTo, Contacts_1.fullName
FROM (
Departments INNER JOIN Contacts ON Departments.departmentID = Contacts.departmentID
) INNER JOIN Contacts AS Contacts_1 ON Contacts.reportsTo = Contacts_1.memberID;

just drag the table in to the visual editor twice. It automatically renames the second instance of the table as "_1".
SELECT Contacts.EmpID, Contacts_1.EmpID AS reportsTo
FROM Contacts INNER JOIN Contacts AS Contacts_1 ON Contacts.SupervisorID=
Contacts_1.EmpID;

Related

multi inner join in where exists invalid identifier

I have the below query with multi joins you can find it on sql fiddle thats giving me invalid DEF.ID
How can I make the below join to read from DEFINITION table
SELECT *
FROM DEFINITION DEF
WHERE EXISTS (SELECT 1
FROM EMPLOYEE E
INNER JOIN MONTHLY_PAYMENT MP ON (MP.ID = E.CODE)
INNER JOIN DEPARTMENT DEP ON (DEP.ID = DEF.ID)
)
By Oracles rules, there are two problems, first DEPARTMENT needs to be related to at least one of the the two tables it is being joined to. And second, DEF cannot be used as a nested inner condition join and so must be used in the WHERE clause instead.
SELECT DEF.ID
FROM DEFINITION DEF
WHERE EXISTS
(
SELECT 1 FROM EMPLOYEE E
INNER JOIN MONTHLY_PAYMENT MP ON (MP.ID=E.CODE)
INNER JOIN DEPARTMENT DEP ON DEP.ID=E.DEP_ID
WHERE DEP.ID=DEF.ID
)

SQL Server query issue - ambiguous column

I have four tables :
Applicant (aid, aname)
entrance_test (Etid, etname)
etest_centre (etcid, location)
etest_details (aid, etid, etcid, etest_dt)
I want to select the number of applicants who have appeared for each test, test center wise.
This is my current query:
select
location, etname, count(Aid) as number of applicants
from
applicant as a
inner join
etest_details as d on a.aid = d.aid
inner join
Entrance_Test as t on t.Etid = d.Etid
inner join
Etest_Centre as c on c.Etcid = d.Etcid
group by
Location, Etname
This is the error I am getting :
Ambiguous column name 'Aid'
You have the column aid in multiple tables, and it doesn't know which to pick from. You should specify which table it is from using the aliases you defined.
In this case, since a.Aid is the same as d.Aid (due to the JOIN), I'm using the a alias, but do keep in mind if location and etname also appear in multiple tables, you need to specify which table it should pick from.
Select c.location, t.etname, Count(a.Aid)
From Applicant As a
Inner Join etest_details As d On a.aid = d.aid
Inner Join Entrance_Test As t On t.Etid = d.Etid
Inner Join Etest_Centre As c On c.Etcid = d.Etcid
Group By c.Location, t.Etname
As a rule of thumb, when you have multiple sources in one query, you should always be explicit about which table it should come from. Even if you're sure it only exists in one of them, it's a good habit to get into to avoid issues like this in the future.
You need to mention the alias in the COUNT clause. Since you are using aliases, it would be better if you use them in the SELECT and GROUP BY sections as well. In this case, it should be :
SELECT a.location,
a.etname,
COUNT(d.Aid)
FROM applicant AS a
INNER JOIN etest_details AS d ON a.aid = d.aid
INNER JOIN Entrance_Test AS t ON t.Etid = d.Etid
INNER JOIN Etest_Centre AS c ON c.Etcid = d.Etcid
GROUP BY a.Location,
a.Etname

t-sql query that returns missing records

I have a query (ContactFormTypesRequired) that returns ContactID and FormTypeID utilizing related tables that are not shown below. This is a list of FormTypes that each Contact should have related to it as a Form.
I need a query that returns Contacts that do not have one or more related forms of the FormTypes specified in the above query.
I've tried a left outer join from Form to ContactsFormTypesRequired on FormTypeID, but the results don't take into account FormTypes that each specific Contact should have.
Please let me know if you have any questions.
Thank you in advance for any suggestions.
I am writing the query this way. This first starts with your query to get needed forms as a CTE, then cross joins them to Contacts to get every needed combination, before left joining to the actual forms.
with NeededForms (<yourqueryhere>)
select distinct c.*
from Contact c cross join
NeededForms nf left outer join
Form F
on nf.FormTypeId = f.FormTypeId left outer join
ContactForm cf
on c.ContactId = cf.ContactId and
f.FormId = cf.FormId
where cf.FormId is null
I'm doing it this way, so you can answer the query of what forms are missing with a very similar query:
with NeededForms (<yourqueryhere>)
select c.*, nf.FormTypeId
from Contact c cross join
NeededForms nf left outer join
Form F
on nf.FormTypeId = f.FormTypeId left outer join
ContactForm cf
on c.ContactId = cf.ContactId and
f.FormId = cf.FormId
where cf.FormId is null
Try this simple query using NOT IN Cluase:
SELECT * FROM Contact
WHERE ContactID IN
(SELECT ContactID FROM ContactForm
INNER JOIN FORM ON ContactForm.FormID=Form.FormID
WHERE FormTypeID=#FormTypeID)
I created ContactFormTypeExist:
SELECT DISTINCT Contact.ContactID, Form.FormTypeID
FROM Contact
INNER JOIN ContactForm
ON Contact.ContactID = ContactForm.ContactID
INNER JOIN Form
ON ContactForm.FormID = Form.FormID
Then joined ContactFormTypesRequired described in the question above to ContactFormTypeExist with outer join to give me the ConactIDs that are missing related FormTypeIDs:
SELECT ContactFormTypesRequired.ConactID
FROM ContactFormTypeExist
RIGHT JOIN ContactFormTypesRequired
ON (ContactFormTypeExist.FormTypeID = ContactFormTypesRequired.FormTypeID)
AND (ContactFormTypeExist.ConactID = ContactFormTypesRequired.ConactID)
WHERE (((ContactFormTypeExist.ConactID) Is Null));
This returns all the ContactID for Contacts missing FormTypes required by their ContactType.

Connecting Three Tables SQL

Hello what I'm trying to do is retrieve values from tables but I'm having a problem with forming a correct SQL statement. This is what i have.
$qry_display = "SELECT a.section_id, b.section_id,b.student_id,c.*
FROM tbl_section AS a
LEFT OUTER JOIN tbl_er AS b On a.section_id = b.section_id
LEFT OUTER JOIN tbl_enroll AS c On b.student_id = c.student_id
WHERE b.student_id=c.student_id
AND a.bname='$branch'";
This is my database structure.
tbl_section:
section_id section_name sy adviser_id level
tbl_er:
student_id section_id
tbl_enroll
student_id fname lname
I'm having problems with forming the proper sql statement would appreciate any help on how to do it right. What i want is to show all students Under a given section.
In what table bname belongs? change bname into sectionname. Try this one,
SELECT c.*
FROM tbl_section a
INNER JOIN tbl_er b
on a.section_ID = b.section_ID
INNER JOIN tbl_enroll c
ON b.student_ID = c.student_ID
WHERE a.sectionname = 'sectionNameHere'

SQL Multi Innerjoin

Hello in my system i have a search page for a student that the admin will be able to view
the students history i have a problem with showing the last name of his/her adviser which is lname_A. This is the code i currently us so far everything is ok except i cant manage to get the lname_a.
$qry_display = "SELECT
a.student_id, a.section_id, a.level, a.photo, a.address, a.father_occupation, a.father_phone, a.father_company, a.mother_occupation, a.mother_phone, a.mother_company,a.gpa,
b.fname, b.sex, b.lname, b.mname, b.birth_date, b.birth_place, b.address, b.father, b.father_degree, b.mother, b.mother_degree,
c.section_name, d.adviser_id , d.lname_a
FROM tbl_er AS a
LEFT OUTER JOIN tbl_enroll AS b ON a.student_id = b.student_id
LEFT OUTER JOIN tbl_section AS c ON a.section_id = c.section_id
LEFT OUTER JOIN tbl_adviser AS d ON a.section_id = d.adviser_id
WHERE a.student_id=".$id." AND a.level='Grade 2'";
Would gladly appreciate any help.
Are you sure you are joining both tables or correct columns?
a.section_id = d.adviser_id
If every student has adviser then you should use inner join rather than left outer join.
When you use left outer join there is chance lname_a is empty when the student doesn't has adviser.