The query works pretty fine, I can view result as a table and sql code on his own without any problem. However then I try to press query design button, it crashed without saying why.
My code is pretty simple, after I added one more left join out of A subquery such error started to appear. This way last left join definitely cause the problem. Tried to join without using subquery but I get problem saying about ambiguous outer join. I'm newbie with access but I heard about several bugs in that program, any suggestion how to fix?
This problem query:
select A.*,targetresp.*
from (
SELECT *
FROM target INNER JOIN ((source INNER JOIN InstanceList
ON source.INFO_SYSTEM_TYPE_CD = InstanceList.INFO_SYSTEM_TYPE_CD)
INNER JOIN (N_table_transform INNER JOIN S2T
ON N_table_transform.N_table = S2T.N_table)
ON source.ID = S2T.source_id)
ON target.id = S2T.target_id ) as A
left join targetresp
on a.target_TableName = targetresp.tablename;
SELECT InstanceList.*
FROM
N_table_transform
INNER JOIN (((S2T INNER JOIN target
ON S2T.target_id = target.Id)
LEFT JOIN targetresp ON target.target_TableName = targetresp.target_TableName)
INNER JOIN (InstanceList
INNER JOIN source ON InstanceList.INFO_SYSTEM_TYPE_CD = source.INFO_SYSTEM_TYPE_CD)
ON S2T.source_id = source.Id)
ON N_table_transform.N_table = S2T.N_table;
I just use the access 2013 designer and build the query.
On the bottom switch between SQL and DESIGNER
Related
I am trying to use a RIGHT and LEFT Join together. I have it working when only left joining one table. But I am trying to now include another table in my left join. It gives me an error saying I am missing an operator. Where am I missing a parenthesis?
FROM qSplit RIGHT JOIN (t_i360_agent AS i LEFT JOIN cmsAgent_Split AS c
ON ((i.LocalDay = c.LocalDay) AND (i.ACDID = c.LOGID))
LEFT JOIN qry_AllNewtables as qry ON (qry.custConvDate = c.LocalDay)
AND (qry.CustAgentLoginName = i.Loginname) ) ON qSplit.SPLIT = c.SPLIT
Don't use LEFT JOIN and RIGHT JOIN together. I imagine that somewhere there could be a query where it makes sense. In practice, I don't think I have ever used them both in the same query, possibly because I write queries using LEFT JOIN.
If you want everything in the agent table, then make it first! And use left join;
FROM t_i360_agent i LEFT JOIN
cmsAgent_Split c
ON i.LocalDay = c.LocalDay AND i.ACDID = c.LOGID LEFT JOIN
qry_AllNewtables qry
ON qry.custConvDate = c.LocalDay AND
qry.CustAgentLoginName = i.Loginname LEFT JOIN
qSplit
ON qSplit.SPLIT = c.SPLIT
It is much easier to follow the intention of the query this way. You are starting with the data that you think is so important that you want to keep all of it, even when JOINs have no matching rows.
Beside the suggestion by Gordon the problem was you miss a join condition for the RIGHT JOIN once I reformat the query was easy to see what was missing.
FROM qSplit
RIGHT JOIN t_i360_agent AS i
-- Need join condition
LEFT JOIN cmsAgent_Split AS c
ON (i.LocalDay = c.LocalDay)
AND (i.ACDID = c.LOGID))
LEFT JOIN qry_AllNewtables as qry
ON (qry.custConvDate = c.LocalDay)
AND (qry.CustAgentLoginName = i.Loginname)
MS Access has been just killing me when It comes to writing my own SQL queries and I hate it's GUI query tool. I gave up trying to combine LEFT JOINS and INNER JOINS now I'm doing all inner join's and i'm still getting murdered. Can someone link to me to a comprehensible MS Access SQL guide and/ or tell me what I am doing wrong here. I've tried putting my code in parenthesis, bracketing my fields. I'm at my wits end.
SELECT answers.*
FROM (((answers
INNER JOIN caseInfo
ON answers.[ABAWDNum] = caseinfo.[ABAWDNum])
INNER JOIN Questions)
ON answers.[questionID] = questions.[questionID])
INNER JOIN responseCodes
ON answers.[responseIDCode] = responseCodes.[responsecode]
I'm not sure if my picture of the error is coming up but it says Sytnax error in FROM clause and it has the parenthesis right after the Questions table is mentioned
enter image description here
EDIT: Database relationships:
enter image description here
Access INNER JOIN can only work on two data sets at once, so if you want to join more than two tables, you have to nest the INNER JOINS using brackets, as described here:
https://learn.microsoft.com/en-us/previous-versions/office/developer/office-2007/bb208854(v=office.12)
From that page it says the syntax to do this is...
SELECT fields FROM table1 INNER JOIN (table2 INNER JOIN [( ]table3 [INNER JOIN [( ]tablex [INNER JOIN …)] ON table3.field3compoprtablex.fieldx)] ON table2.field2compoprtable3.field3) ON table1.field1compoprtable2.field2;
I believe this will give you all fields from the table answers that meet your join conditions.
SELECT answers.*
FROM answers
INNER JOIN caseInfo
ON answers.[ABAWDNum] = caseinfo.[ABAWDNum]
INNER JOIN Questions
ON answers.[questionID] = questions.[questionID]
INNER JOIN responseCodes
ON answers.[responseIDCode] = responseCodes.[responsecode]
Just to humor Access I used the GUI/Design mode to build a query.
Here's the query I got that ran. From a table with 674 records, it only returned 6 answers, but that is a separate problem that I have to deal with. At least this thing runs.
But I have a feeling once I start asking for left joins I'm in trouble. <_< F* access.
SELECT answers.*
FROM (Questions
INNER JOIN (CaseInfo
INNER JOIN answers ON CaseInfo.ABAWDNum = answers.ABAWDNum)
ON Questions.questionID = answers.questionID)
INNER JOIN responseCodes ON (Questions.questionID = responseCodes.questionCode)
AND (answers.responseIDCode = responseCodes.responseCode);
EDIT:
I used the GUI/Designer again to try to make a query that was closer to what I needed, which is all of the answers in the answers table plus just a few things from the question table, caseinfo table, and responsecode tables , and this is what I got
SELECT
CaseInfo.ABAWDNum, CaseInfo.Admin, CaseInfo.Unit,
CaseInfo.[ASV-DRV Completed By], CaseInfo.reviewerID, CaseInfo.[Recieved Date],
CaseInfo.[Date Reviewed],
Questions.Question,
answers.responseIDCode, responseCodes.responseDescription, answers.comments
FROM
CaseInfo
RIGHT JOIN
((Questions
RIGHT JOIN
answers ON Questions.questionID = answers.questionID)
LEFT JOIN
responseCodes ON answers.responseIDCode = responseCodes.responseCode)
ON CaseInfo.ABAWDNum = answers.ABAWDNum;
This is really insane. I'm going to have to study this really hard. It looks like access is very particular on how it wants it's joins, but this seems to give me the correct results. 674 records returned.
The following code works in Sage200.
SELECT bcs.BomReference
,bcs.DateTimeCosted
,bcs.TotalCost
FROM (
SELECT BomReference
,Max(DateTimeCosted) AS MaxDate
FROM NDM_Sage200.dbo.BomCostSession BomCostSession
GROUP BY BomReference
) AS ldc
INNER JOIN BomCostSession AS bcs ON bcs.BomReference = ldc.BomReference
AND bcs.DateTimeCosted = ldc.MaxDate
ORDER BY BomReference
As soon as I try extending this with an INNER JOIN to another table to get more columns (using BomReference), I get the error message:
Could not add the table (.
See below for example of modified code; I have to use 2 joins to get to the table I need, but have the same error whatever I join onto the working code.
SELECT bcs.BomReference, bcs.DateTimeCosted, bcs.TotalCost, BomBuildProduct.StockDescription
FROM (
SELECT BomReference,
Max(DateTimeCosted) AS MaxDate
FROM NDM_Sage200.dbo.BomCostSession BomCostSession
GROUP BY BomReference
) AS ldc
INNER JOIN
BomCostSession as bcs
ON bcs.BomReference = ldc.BomReference AND
bcs.DateTimeCosted = ldc.MaxDate
***** Fails when adding INNER JOIN here *****
INNER JOIN
BomBuildPackage
ON BomCostSession.BomBuildPackageID = BomBuildPackage.BomBuildPackageID
INNER JOIN
BomBuildProduct
ON BomBuildPackage.BomRecordID = BomBuildProduct.BomRecordID
ORDER BY BomReference
What am I doing wrong ? I need to expand the query with data from several tables.
I also think that when using MSQuery on the section that works, it offers no options to add any tables - this makes it rather difficult to try options.
Why ?
The issue with MSQuery is that it attempts to display your query graphically in it's design view, this works OK for simple queries but not for complex queries which usually generates the could not add table message. The way I found around this is to treat your query as one big sub query inside a wrapper query, this forces MSQuery to give up the design view and work as pure SQL text.
Another issue might be that for one table you have the full path but not the others, is it correct for the table you have included it and does it need to be used on the other tables.
Here is an example of the changes I think you should make:
SELECT * FROM (
SELECT bcs.BomReference
,bcs.DateTimeCosted
,bcs.TotalCost
,BomBuildProduct.StockDescription
FROM
(SELECT BomReference
,Max(DateTimeCosted) AS MaxDate
FROM NDM_Sage200.dbo.BomCostSession BomCostSession
GROUP BY BomReference) AS ldc
INNER JOIN NDM_Sage200.dbo.BomCostSession AS bcs ON bcs.BomReference = ldc.BomReference
AND bcs.DateTimeCosted = ldc.MaxDate
INNER JOIN NDM_Sage200.dbo.BomBuildPackage ON BomCostSession.BomBuildPackageID = BomBuildPackage.BomBuildPackageID
INNER JOIN NDM_Sage200.dbo.BomBuildProduct ON BomBuildPackage.BomRecordID = BomBuildProduct.BomRecordID) x
ORDER BY BomReference
SELECT
Trs.itemID, Trs.imtName, Trs.sumQty, Sum(whiQty)
FROM
((SELECT
trsitemID AS itemID, trsimtName AS imtName,
Sum(trsQty) As sumQty
FROM
tblTransactionSub AS T
WHERE
trstraID = 1231
AND trsActive = True
GROUP BY
trsitemID, trsimtName) AS Trs
INNER JOIN
tblWarehouseItem AS WHI ON Trs.itemID = WHI.whiitemID)
RIGHT JOIN
WHI ON Trs.trswhiID = WHI.whiID
WHERE
whiActive = True
AND whiCansel = False
AND whiwrhID = 19
GROUP BY
Trs.itemID,Trs.imtName, Trs.sumQty
HAVING
SUM(whiQty) < Trs.sumQty
If you please help me me out since I am new to SQL commands I can not easily find my mistake.
Thanks in advance
The error that occurred when I added the Right Join is:
Join expression not supported
In MS Access, you have to use parenthesises with multiple joins:
select ...
from
((table1
... join table2 on ...)
... join table3 on ...)
... join tableN
/edit/
As OP question changes its syntax often, then my answer seems out of place :) Initially there were no parens there.
About RIGHT JOIN: You need to use table name (or entire subselect) after JOIN keyword, not skip it or use some other alias. Your query part
RIGHT JOIN
WHI ON Trs.trswhiID = WHI.whiID
currently uses alias WHI, which is wrong in two ways: 1) it is not table name 2) it is already used. You need something like this:
RIGHT JOIN
tblWarehouseItem AS WHI2 ON Trs.trswhiID = WHI2.whiID
It could be possible that MS Access restricts your kind of JOINs usage (like INNER join should not come after LEFT join); I have currently no possibility to check precise rules.
Your problem is that you have no table name after the RIGHT JOIN:
RIGHT JOIN
ON Trs.trswhiID = WHI.whiID
Should be:
RIGHT JOIN YOURTABLENAMEHERE as alias
ON Trs.trswhiID = WHI.whiID
However, you have already defined Trs and Whi, so I have no idea what table you want there, or why. Perhaps you just want to change the INNER JOIN to a LEFT JOIN or RIGHT JOIN.
I am trying to understand why my query(below) displays an error message in MS Access Sql query editor(sqlview) when I run it.
SELECT *
FROM tblUSPS
INNER JOIN tblProductUSPS
ON tblProductUSPS.[PRODUCTUSPS_USPS] = tblUSPS.[USPS_CODE]
INNER JOIN tblAttribute
ON tblUSPS.USPS_ID = tblAttribute.ATTRIBUTE_USPSID
As far as I know the script below if I delete either of the INNER join lines. For instance, this script runs with no errors
SELECT *
FROM tblUSPS
INNER JOIN tblProductUSPS
ON tblProductUSPS.[PRODUCTUSPS_USPS] = tblUSPS.[USPS_CODE]
And so does this
SELECT *
FROM tblUSPS
INNER JOIN tblAttribute ON tblUSPS.USPS_ID = tblAttribute.ATTRIBUTE_USPSID
But when I combine, something goes wrong and I am unable to find it so I would like some help identifying this please.
Access has strong opinions on parentheses.
SELECT *
FROM
(tblUSPS
INNER JOIN tblProductUSPS
ON tblProductUSPS.[PRODUCTUSPS_USPS] = tblUSPS.[USPS_CODE] )
INNER JOIN tblAttribute
ON tblUSPS.USPS_ID = tblAttribute.ATTRIBUTE_USPSID