问题
Table 1: SalesforceReportWeekly ; Fields: [Opportunity: Store Number], [Target Circuit Completion (FOC)], ...
Table 2: SentSalesforceReportWeekly Fields: [Opportunity: Store Number], [Target Circuit Completion (FOC)], ...
I need a query that will work in MS Access which will show all rows in the SalesforceReportWeekly that do not have a corresponding row in the SentSalesforceReportWeekly. using the [Opportunity: Store Number] AND the [Target Circuit Completion (FOC)] fields. records where both the [Opportunity: Store Number] and the [Target Circuit Completion (FOC)] are the same need to be omitted from my result view. a record in SalesforceReportWeekly with an identical store number but a different [Target Circuit Completion (FOC)] to a record in SentSalesforceReportWeekly needs to be included in my results view. This has to work in MS Access. I feel like I have tried everything:
Attempt One: for some reason this returns almost the entire table...
SELECT *
FROM [SalesforceReportWeekly]
LEFT OUTER JOIN [SentSalesforceReportWeekly]
ON ([SalesforceReportWeekly].[Opportunity: Store Number] = [SentSalesforceReportWeekly].[Opportunity: Store Number] AND [SalesforceReportWeekly].[Target Circuit Completion (FOC)] = [SentSalesforceReportWeekly].[Target Circuit Completion (FOC)])
WHERE [SentSalesforceReportWeekly].[Target Circuit Completion (FOC)] IS NULL AND [SentSalesforceReportWeekly].[Opportunity: Store Number] IS NULL;
Attempt Two: returns a result view with tons of null rows except for the rows which are in the sent table.
SELECT *
FROM [SalesforceWeeklyReportFutureInstalls]
LEFT JOIN [SentInstallScheduled] ON ([SalesforceWeeklyReportFutureInstalls].[Opportunity: Store Number] = [SentInstallScheduled].[StoreNumber] AND [SalesforceWeeklyReportFutureInstalls].[Target Circuit Completion (FOC)] = [SentInstallScheduled].[TargetCircuitCompletionFOC])
UNION
SELECT *
FROM [SalesforceWeeklyReportFutureInstalls]
RIGHT JOIN [SentInstallScheduled] ON ([SalesforceWeeklyReportFutureInstalls].[Opportunity: Store Number] = [SentInstallScheduled].[StoreNumber] AND [SalesforceWeeklyReportFutureInstalls].[Target Circuit Completion (FOC)] = [SentInstallScheduled].[TargetCircuitCompletionFOC]);
I have spent hours combing through other SO questions, i have found other similar problems but none exactly like mine(two tables excluding results of one table from another on two fields) and haven't been able to successfully adapt their solutions to fit my needs. Any and all help will be very appreciated!
回答1:
The standard way to find rows which don't exists in another table utilizes NOT EXISTS:
SELECT *
FROM [SalesforceReportWeekly]
WHERE NOT EXISTS
(SELECT * FROM [SentSalesforceReportWeekly]
WHERE [SalesforceReportWeekly].[Opportunity: Store Number] = [SentSalesforceReportWeekly].[Opportunity: Store Number]
AND [SalesforceReportWeekly].[Target Circuit Completion (FOC)] = [SentSalesforceReportWeekly].[Target Circuit Completion (FOC)]
)
来源:https://stackoverflow.com/questions/30987844/ms-access-query-to-display-rows-of-table1-which-are-not-in-table2-using-two-fiel