Access SQL: syntax error in join statement - sql

I'm trying to execute a SQL statement, but I get an error that says there's a syntax error in the join.
I'm executing the following query:
SELECT
T.Omschrijving as Team, P.Beschrijving AS Proces,
Round(Sum(P.Aantal), 2) AS Prognose, P.Maand AS PrognoseMaand,
P.jaar, A.Omschrijving AS Fonds
FROM
tblPrognose AS P, tblTeam AS T, tblAfdeling AS A
LEFT JOIN
(SELECT
RL.Omschrijving, I.TEAMID, I.PROCESOMS,
SUM(NORMUREN_I) AS Instroom, I.Maand, I.Jaar
FROM
tblInstroom AS I
LEFT JOIN
(SELECT
T.Omschrijving, R.GPSTeam, R.Procesoms
FROM
tblRollen AS R, tblTeam AS T
WHERE
T.TeamID = R.TeamID) AS RL ON (I.TEAMID = RL.GPSTEAM AND I.PROCESOMS = RL.PROCESOMS)
GROUP BY
I.TEAMID, I.Procesoms, I.maand, I.jaar, RL.Omschrijving) AS tblR ON (tblR.Omschrijving = T.Omschrijving) AND (tblR.Procesoms = P.Beschrijving) AND (tblR.Maand = P.Maand) AND (tblR.jaar = P.jaar)
WHERE
P.KPI = 'Instroom'
AND P.Maand BETWEEN Month(P.Datum_vanaf) AND Month(P.Datum_tot)
AND P.jaar BETWEEN year(P.Datum_vanaf) AND year(P.Datum_tot)
AND P.TeamID = T.TeamID
AND A.AfdelingID = T.AfdelingID
AND NOT Nz(P.Beschrijving, '') = ''
GROUP BY
T.Omschrijving, P.Maand, P.jaar, P.Beschrijving, A.Omschrijving
I have tested if the error is within the JOINs but it isn't. If I execute the SELECTs seperately they work just fine and give me the correct results.
Any help would be appreciated!

Related

IN Query Expression . (Error 3075). MS ACESSS

SQL Code: SELECT Alan1, Alan2, Alan3, Alan4, A.A_Ihbar_Sayısı, B.B_Ihbar_Sayısı, C.C_Ihbar_Sayısı FROM A INNER JOIN B ON A.Alan4 = B.Alan4 AND A.Alan3 = B.Alan3 AND A.Alan2 = B.Alan2 INNER JOIN C ON B.Alan4 = C.Alan4 AND B.Alan3 = C.Alan3 AND B.Alan2 = C.Alan2;
I get error in query expression . (Error 3075).I'm sure I didn't make a syntax error. How can I fix this situation?
MS Access has arcane syntax requirements, such as parentheses in the FROM clause for multiple JOINs:
SELECT Alan1, Alan2, Alan3, Alan4, A.A_Ihbar_Sayısı, B.B_Ihbar_Sayısı, C.C_Ihbar_Sayısı FROM (A INNER JOIN
B
ON A.Alan4 = B.Alan4 AND A.Alan3 = B.Alan3 AND A.Alan2 = B.Alan2
) INNER JOIN
C
ON B.Alan4 = C.Alan4 AND B.Alan3 = C.Alan3 AND B.Alan2 = C.Alan2;

3 inner joins and a left join in my FROM statement

I am debugging some code for an MS Access program with a SQL backend. One of the queries is producing the following error: "the expression on click you entered as the event property setting produced the following error:
cannot join on Memo, OLE, or Hyperlink Object (master_export.item=vw_items.Item)"
This is the original code:
SELECT master_export_pt.commitment_number AS [Commitment Number],
Sum(master_export_pt.receipt_amount) AS Amount,
LocalLocation.Location_Name AS Child_Location_Name,
LocalLocation_1.Location_Name AS Parent_Location_Name,
BypassLocation.JNL_Sales AS ByPassSales,
BypassLocation.JNL_COGS AS ByPassCogs,
master_export_pt.order_type
FROM (((master_export_pt
INNER JOIN vw_items ON master_export_pt.item = vw_items.Item)
INNER JOIN BypassLocation ON master_export_pt.location_id = BypassLocation.location_id)
INNER JOIN LocalLocation ON BypassLocation.location_id = LocalLocation.Location_ID)
LEFT JOIN LocalLocation AS LocalLocation_1
ON LocalLocation.parent_location_id = LocalLocation_1.Location_ID
and tried changing the first JOIN to:
FROM (((master_export_pt, vw_items)
where (master_export_pt.item = vw_items.Item))
INNER JOIN BypassLocation ON master_export_pt.location_id = BypassLocation.location_id)
INNER JOIN LocalLocation ON BypassLocation.location_id = LocalLocation.Location_ID)
LEFT JOIN LocalLocation AS LocalLocation_1
ON LocalLocation.parent_location_id = LocalLocation_1.Location_ID
but now i get the "Syntax error in JOIN operation" error message. Is there a way to re-write the query without using nested JOIN statements?
Thank you to all who responded. I tried the following code and it seems to have done the trick:
SELECT
xxx.commitment_number AS [Commitment Number],
Sum(xxx.receipt_amount) AS Amount,
xxx.Location_Name AS Child_Location_Name,
xxx.Location_Name AS Parent_Location_Name,
xxx.JNL_Sales AS ByPassSales,
xxx.JNL_COGS AS ByPassCogs,
xxx.order_type
FROM (
SELECT * FROM
master_export_pt AS mept, vw_items AS vi, BypassLocation AS bl, LocalLocation AS ll
WHERE mept.item = vi.Item
AND mept.location_id = bl.location_id
AND bl.location_id = ll.Location_ID) AS xxx
LEFT OUTER JOIN LocalLocation AS ll1 ON xxx.parent_location_id = ll1.Location_ID
GROUP BY
xxx.commitment_number,
xxx.Location_Name,
ll1.Location_Name,
xxx.JNL_Sales,
xxx.JNL_COGS,
xxx.order_type;

MS Access: SQL View error

I'm getting an error when pasting in a raw SQL query into Access's SQL View. I know Access syntax is a bit special but I can't figure out what it's asking for. The error says: Syntax error (missing operator) in query expression '(jobmatl.suffix = job.suffix) AND (job.job = jobmatl.job) INNER ...................... AS ibl ON jobmatl.item = ibl.item AND job.whse = ibl.whse. The error mentions everything in between what I've written.
SELECT
job.job,
job.suffix,
job.job_date,
job.item AS FG,
jobmatl.item,
job.whse,
ibl.sumofqtyonhand,
ibl.whse
FROM
job
INNER JOIN jobmatl ON (jobmatl.suffix = job.suffix) AND (job.job = jobmatl.job)
INNER JOIN (
(SELECT
i.item,
SUM(i.qty_on_hand) AS sumofqtyonhand,
i.whse
FROM
Item_by_Location_LP_ALL AS i
WHERE
i.hold_flag != 1
GROUP BY
i.item,
i.whse
)) AS ibl ON jobmatl.item = ibl.item AND job.whse = ibl.whse
WHERE
(((job.job_date)=Date()-(DatePart("w",Date(),2,1)-1)));
The FROM should look like this for MS Access:
FROM (job INNER JOIN
jobmatl
ON jobmatl.suffix = job.suffix AND job.job = jobmatl.job
) INNER JOIN
(SELECT i.item, SUM(i.qty_on_hand) AS sumofqtyonhand, i.whse
FROM Item_by_Location_LP_ALL AS i
WHERE i.hold_flag <> 1
GROUP BY i.item, i.whse
) AS ibl
ON jobmatl.item = ibl.item AND job.whse = ibl.whse;
MS Access requires extra parentheses for each JOIN. In addition, you have to levels of parentheses -- and I don't know if that is allowed.

UPDATE query with multiple INNER JOINs (syntax error)

I'm trying to run an update query with multiple join statements in VBA, but I keep getting "missing operator" errors. Same happens when I try to run the SQL in query builder. The SQL is:
UPDATE TBL_DocReview_SingleStudy
INNER JOIN TBL_LOA
ON TBL_LOA.MemberName = TBL_DocReview_SingleStudy.Member
INNER JOIN TBL_STUDY
ON TBL_STUDY.StudyName = TBL_DocReview_SingleStudy.Study
SET TBL_DocReview_SingleStudy.DateLOA2 = TBL_LOA.Status
WHERE TBL_STUDY.CDB = 'B'
Can anyone see what I'm doing wrong?
Try:
UPDATE (TBL_DocReview_SingleStudy
INNER JOIN TBL_LOA
ON TBL_LOA.MemberName = TBL_DocReview_SingleStudy.Member)
INNER JOIN TBL_STUDY
ON TBL_STUDY.StudyName = TBL_DocReview_SingleStudy.Study
SET TBL_DocReview_SingleStudy.DateLOA2 = TBL_LOA.Status
WHERE TBL_STUDY.CDB = 'B'
Try the following:
UPDATE s
SET TBL_DocReview_SingleStudy.DateLOA2 = TBL_LOA.STATUS
FROM TBL_DocReview_SingleStudy S
INNER JOIN TBL_LOA ON TBL_LOA.MemberName = TBL_DocReview_SingleStudy.Member
INNER JOIN TBL_STUDY ON TBL_STUDY.StudyName = TBL_DocReview_SingleStudy.Study
WHERE TBL_STUDY.CDB = 'B'

Getting error: "Column ambiguously defined "

I am running the following query and getting column ambiguously defined :
SELECT
S.SUB_ID
,M.FLEETID
,M.TGID
,M.TGNO
,R.TGTYPE
,R.MODEID
,COUNT(1)
FROM
INF_SUBSCRIBER_ALL S
INNER JOIN
INF_TALKGROUP_MEMBER M ON S.SUB_ID = M.SUBID
INNER JOIN
INF_TALKGROUP_MODE_RELATION R ON M.TGID = R.TGID
INNER JOIN
INF_TALKGROUP_MODE_RELATION R ON M.FLEETID = R.FLEETID
WHERE
S.SUB_STATE = 'B01'
AND M.STATUS = 'M01'
GROUP BY
S.SUB_ID, M.FLEETID, M.TGID, M.TGNO, R.TGTYPE, R.MODEID;
You have used aliase-R twice, that is why the error,
You dont need to include a table twice into JOINs for adding another condition(M.FLEETID = R.FLEETID),
You can give the corresponding condition in the first occurce itselt using AND operator.
SELECT
S.SUB_ID
,M.FLEETID
,M.TGID
,M.TGNO
,R.TGTYPE
,R.MODEID
,COUNT(1)
FROM
INF_SUBSCRIBER_ALL S
INNER JOIN
INF_TALKGROUP_MEMBER M ON S.SUB_ID = M.SUBID
INNER JOIN
INF_TALKGROUP_MODE_RELATION R ON M.TGID = R.TGID AND M.FLEETID = R.FLEETID
WHERE
S.SUB_STATE = 'B01'
AND M.STATUS = 'M01'
GROUP BY
S.SUB_ID, M.FLEETID, M.TGID, M.TGNO, R.TGTYPE, R.MODEID;
Hope this helps.