问题
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 ?
回答1:
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
来源:https://stackoverflow.com/questions/31516811/could-not-add-the-table