Just need to ask for your help, I have been having some problems with MS Access, I'm trying to create a nested INNER JOIN to perform a query. All of the fields that I need are showing up, however when I try to add a new entry it gives me an error
Can't Add Records Join key of table is not in record set.
Here's my code:
SELECT
Applicant_ID, Complete_Name, Date_of_Birth, Date_of_Application, Gender,
City_Address, Position_Applied, Civil_Status, Age, Educational_Attainment,
Table_JuniorRecruiter.Junior_Recruiter_ID, Junior_Recruiter_Name, Exam_Remarks,
Table_Exam.Exam_Number
FROM
(Table_Applicant
INNER JOIN
Table_Exam ON Table_Applicant.Exam_Number = Table_Exam.Exam_Number)
INNER JOIN
Table_JuniorRecruiter ON Table_Applicant.Junior_Recruiter_ID = Table_JuniorRecruiter.Junior_Recruiter_ID;
The brackets you have round the first table and its first inner join give a syntax error. Between the ( and the ) needs to be a complete SELECT statement, and you start off with a table name, which no valid SELECT statement will do.
Now I don't actually know what you're trying to do, but this will be valid SQL:
SELECT
Applicant_ID, Complete_Name, Date_of_Birth, Date_of_Application, Gender,
City_Address, Position_Applied, Civil_Status, Age, Educational_Attainment,
Table_JuniorRecruiter.Junior_Recruiter_ID, Junior_Recruiter_Name, Exam_Remarks,
Table_Exam.Exam_Number
FROM
Table_Applicant
INNER JOIN
Table_Exam ON Table_Applicant.Exam_Number = Table_Exam.Exam_Number
INNER JOIN Table_JuniorRecruiter
ON Table_Applicant.Junior_Recruiter_ID
= Table_JuniorRecruiter.Junior_Recruiter_ID
Here I've just taken out the ( and the ). It should be valid SQL (provided the table and column names are correct) but it might not be the actual query you want.
However, I can't see what you'd gain by making the table_applicant and table_exam join a nested subquery: doing that looks totally un-necessary.
Related
for one particular query I've written up the ORA-00904 error has occurred
SELECT '103040698' as StudID, M.Title, R.SHORTDESC, C.COLOURNAME
FROM MOVIE0698 M
INNER JOIN RATING0698 R
ON M.SHORTDESC = R.SHORTDESC
INNER JOIN COLOURTYPE0698 C
ON M.COLOURNAME = C.COLOURNAME
ORDER BY Title ASC;
specfically for ON **M.**SHORTDESC = R.SHORTDESC
However, the previous query I had just written beforehand that used an INNER JOIN as well
SELECT '103040698' as StudID, A.FullName, M.Title, M.RelYear
FROM CASTING0698 C
INNER JOIN ACTOR0698 A
ON C.ActorNo = A.ActorNo
INNER JOIN MOVIE0698 M
ON C.MovieNo = M.MovieNo
ORDER BY RelYear DESC;
this query runs perfectly, so I'm just unclear what I've done wrong.
From what I remember INNER JOINs are written with the child table first then the parent table e.g. ON C.ActorNo = A.ActorNo but I could be wrong since I know some software will accept either way while others are sensitive to it.
for context, I'm using SQLJunior for my school work
here is the whole error
ON M.shortdesc = R.shortdesc
*
Error at line 4: ORA-00904: "M"."SHORTDESC": invalid identifier
In plain English the error message means "the MOVIES0698 table doesn't have a column called SHORTDESC"
Remember that Oracle column names are only case insensitive if they are not enclosed in quotes, or were quoted allcaps. If a column name is enclosed in quotes, and is not all uppercase, when it is created then it must forced more be referred to using quotes
SHORTDESC -- not case sensitive, you could write SELECT shortDesc
"SHORTDESC" -- not case sensitive, because it's all caps anyway, can write SELECT shortdesc
"ShortDesc" -- case sens, must refer to this column using quotes forever more: SELECT "ShortDesc"... JOIN ON M."ShortDesc" etc
In summary, never enclosed oracle column names in quotes, for any reason. If you want to call a column something that is a reserved word, call it something else
If movies has no such column you have to join on a different column (SHORTDESC seems like it would be a text column, which probably makes for a poor join target anyway; is there no other column that is intended to relate movies and ratings better? Such as a "movieid" in the ratings table? It seems to me like one movie could have one or more ratings (some critics rate it high, others low, and you average the scores of many ratings..?)
I was using the wrong column names to join these tables together, i was writing it as
SELECT '103040698' as StudID, M.Title, R.SHORTDESC, C.COLOURNAME
FROM MOVIE0698 M
INNER JOIN RATING0698 R
ON M.SHORTDESC = R.SHORTDESC
INNER JOIN COLOURTYPE0698 C
ON M.COLOURNAME = C.COLOURNAME
ORDER BY Title ASC;
instead of
SELECT '103040698' as StudID, M.Title, R.SHORTDESC, C.COLOURNAME
FROM MOVIE0698 M
INNER JOIN RATING0698 R
ON M.ratingcode= R.ratingcode
INNER JOIN COLOURTYPE0698 C
ON M.colourcode= C.colourcode
ORDER BY Title ASC;
TL:DR
I had to use the proper foreign/primary key for INNER JOIN
I am trying to write a SQL Select statement to port some data from a Sybase environment to a MongoDB environment and I'm just trying to figure out the correct syntax to involve three different tables.
Basically what I need to do is do an INNER JOIN on two tables, and then do a matching check against a 3rd table. The three table names are "ar_notes", "customer_service_xref" and "service_notes_details"
This is what I tried:
SELECT * FROM ar_notes arn
LEFT JOIN customer_service_xref csx ON arn.customer_service_xref_id = csx.id_number
WHERE arn.visit_date = service_notes_details.date_of_visit
This doesn't work. I get a correlation error.
What should the syntax look like when involving three tables like this?
You said you need an INNER JOIN among three tables but your query does a LEFT JOIN between two and tries another join in the WHERE clause without refering that table in the FROM clause.
To just fix your query:
SELECT *
FROM service_notes_details snd, ar_notes arn
INNER JOIN customer_service_xref csx ON arn.customer_service_xref_id = csx.id_number
WHERE arn.visit_date = snd.date_of_visit
This is what you should use in current SQL syntax:
SELECT *
FROM ar_notes arn
INNER JOIN customer_service_xref csx ON arn.customer_service_xref_id = csx.id_number
INNER JOIN service_notes_details ON arn.visit_date = service_notes_details.date_of_visit
To be clear, this will only return lines in ar_notes that have corresponding values in customer_service_xref (joining by customer_service_xref_id) and in service_notes_details(joining by visit_date). Your original query, using LEFT JOIN would return lines from ar_notes even if there was no matching customer_service_xref_id.
I have 3 tables:
1 - tblMembers_Info
2 - a junction table
3 - tblCourses
I need to query the members who haven't done a specific course.
After trying to do it manually I gave MS Access "Query Wizard" a try. I ended up with :
A saved query as Query1:
// That one query who did the course
SELECT tblMembers_Info.*, tblCourses.CourseName
FROM tblMembers_Info
INNER JOIN
(tblCourses INNER JOIN tblMembers_Courses
ON tblCourses.IDCourses = tblMembers_Courses.IDCourses)
ON tblMembers_Info.Members_ID = tblMembers_Courses.Members_ID
WHERE (tblCourses.CourseName) In ('NameOftheCourse');
2nd query using the saved Query1:
SELECT tblMembers_Info.Members_ID, tblMembers_Info.FirstName, tblMembers_Info.LastName
FROM tblMembers_Info
LEFT JOIN [Query1]
ON tblMembers_Info.[Members_ID] = Query1.[Members_ID]
WHERE (((Query1.Members_ID) Is Null));
How can I replace the Query1 in the second query with the full query instead of using a QueryDef (the saved query "Query1")?
Also, there's a better way for sure to write that query, I would really appreciate any help.
You can simply replace LEFT JOIN [Query1] with LEFT JOIN (...) AS [Query1] where ... should be the SQL of the first query, without the ending ;.
But I think in your specific case the use of NOT IN might give a better performance to get the same results:
SELECT tblMembers_Info.Members_ID, tblMembers_Info.FirstName, tblMembers_Info.LastName
FROM tblMembers_Info
WHERE tblMembers_Info.[Members_ID] NOT IN (
SELECT tblMembers_Info.[Members_ID]
FROM ((tblMembers_Info
INNER JOIN tblMembers_Courses
ON tblMembers_Info.Members_ID = tblMembers_Courses.Members_ID)
INNER JOIN tblCourses
ON tblCourses.IDCourses = tblMembers_Courses.IDCourses)
WHERE tblCourses.CourseName = 'NameOftheCourse'
);
I have a great problem with joining two MSSQL databases together (on the same server) using LEFT JOIN. I run this SQL from the database OLAB_DB and get the error:
The multi-part identifier "OLAP_DB.dbo.OLAP_invoice.UserID" could not be bound.
There seem to be a problem with the DB not being able to find itself, and I have no idea of how to solve this. I have double and triple checked the spelling and rewrote the SQL several times, but now I just have to give up and ask for help :(
This doesn't work:
SELECT TOP 200
COALESCE(LTRIM(RTRIM(contact_db.dbo.ContactTable.EmailAdr)),LTRIM(RTRIM(contact_db.dbo.CustomerTable.EmailAdr))) AS EMAIL,
OLAP_invoice.OrdreLinjeID AS ORDERNO
OLAP_invoice.SalgsPris AS PRICE,
OLAP_invoice.UserID AS CONTACTID
FROM OLAP_invoice,contact_db.dbo.CustomerTable
LEFT JOIN contact_db.dbo.ContactTable
ON OLAP_DB.dbo.OLAP_invoice.UserID = contact_db.dbo.ContactTable.UserID
WHERE contact_db.dbo.CustomerTable.ClientID = OLAP_invoice.ClientID
But skipping the left join and just getting data from the two different databases works just fine.
This works just fine:
SELECT TOP 200
LTRIM(RTRIM(contact_db.dbo.CustomerTable.EmailAdr)) AS EMAIL,
LTRIM(RTRIM(contact_db.dbo.ContactTable.UserID)) AS EMAIL2,
OLAP_invoice.OrdreLinjeID AS ORDERNO
OLAP_invoice.SalgsPris AS PRICE,
OLAP_invoice.UserID AS CONTACTID
FROM OLAP_invoice,contact_db.dbo.CustomerTable
WHERE contact_db.dbo.CustomerTable.ClientID = OLAP_invoice.ClientID
AND contact_db.dbo.ContactTable.UserID = OLAP_invoice.UserID
The reason I need the LEFT JOIN, is because some orders are not registered with a UserID (only ClientID).
I have checked the access rights and there is no problem to access the fields individually, the problem occurs when I have to compare the values in the LEFT JOIN and specifies the absolute path.
Do you have any idea of what can be wrong?
You can try this:
SELECT TOP 200 COALESCE(LTRIM(RTRIM(contact_db.dbo.ContactTable.EmailAdr)),LTRIM(RTRIM(contact_db.dbo.CustomerTable.EmailAdr))) AS EMAIL,
OLAP_invoice.OrdreLinjeID AS ORDERNO
OLAP_invoice.SalgsPris AS PRICE,
OLAP_invoice.UserID AS CONTACTID
FROM OLAP_invoice LEFT JOIN contact_db.dbo.ContactTable ON OLAP_DB.dbo.OLAP_invoice.UserID = contact_db.dbo.ContactTable.UserID, contact_db.dbo.CustomerTable
WHERE contact_db.dbo.CustomerTable.ClientID = OLAP_invoice.ClientID
You are selecting from OLAP_invoice, but joining to OLAP_DB.dbo.OLAP_invoice. You need to qualify the table exactly the same every time, or use an alias.
So either:
FROM OLAP_DB.dbo.OLAP_invoice
left join contact_db.dbo.ContactTable
on OLAP_DB.dbo.OLAP_invoice...
OR
FROM ROM OLAP_invoice
LEFT JOIN ontact_db.dbo.ContactTable
ON OLAP_invoice...
Or you could use an alias as well, less typing that way.
FROM OLAP_invoice OI
LEFT JOIN ontact_db.dbo.ContactTable CT
ON OI...
Personally, I try to avoid having multiple tables in my FROM without intentionally specifying the type of join.
I find something like this to be much more readable and maintainable :
SELECT TOP 200
COALESCE( LTRIM(RTRIM(con.EmailAdr)), LTRIM(RTRIM(cus.EmailAdr)) ) AS EMAIL,
i.OrdreLinjeID AS ORDERNO
i.SalgsPris AS PRICE,
i.UserID AS CONTACTID
FROM OLAP_invoice i
LEFT JOIN contact_db.dbo.CustomerTable cus ON cus.ClientID = i.ClientID
LEFT JOIN contact_db.dbo.ContactTable con ON con.UserID = i.UserID
Have tried the below SQL in MS Access but cannot seem to get it working, anyone got a better idea?
SELECT top 4 Student.STUDENT_DEGREE, Student.STUDENT_SEX,STUDENT_GROUP_ID,STUDENT_GROUP_ID2,RESULT_MARK
FROM (((Student)
INNER JOIN Result ON Student.STUDENT_ID=Result.RESULT_STUDENT_ID)
INNER JOIN Group ON RESULT_GROUP_ID = GROUP_ID)
where STUDENT_GROUP_ID <> ''
order by Result.RESULT_MARK desc;
Whenever i run this i just get the error:
Syntax error in FROM clause
Group is a reserved word. Enclose that name in square brackets to avoid confusing the db engine. You can also assign an alias for the table name.
FROM
(Student
INNER JOIN Result
ON Student.STUDENT_ID=Result.RESULT_STUDENT_ID)
INNER JOIN [Group] AS g
ON Result.RESULT_GROUP_ID = g.GROUP_ID
I had to guess which tables contain those fields in the last ON clause. If you set up the joins in Design View of the Access query designer, it will help you get the names right. It will also add the parentheses which the db engine requires for any query which includes more than one join.
Also qualify the table sources for the field names in your SELECT list and elsewhere in the query. Here again, the query designer can supply the correct names for you.
Remove the extra set of parentheses around Student:
SELECT top 4 Student.STUDENT_DEGREE,Student.STUDENT_SEX,STUDENT_GROUP_ID,STUDENT_GROUP_ID2,RESULT_MARK
FROM ((Student
INNER JOIN Result ON Student.STUDENT_ID=Result.RESULT_STUDENT_ID)
INNER JOIN Group ON RESULT_GROUP_ID = GROUP_ID)
where STUDENT_GROUP_ID <> ''
order by Result.RESULT_MARK desc;