Excluding Records from Query

QandAdam

New Member
Joined
Jan 12, 2019
Messages
30
I have an Access Database Table for Sales which lists multiple records and the Salesperson associated. Example below:

BatchSalesperson NameDate Allocated
1234Alan Smith01/02/2019
1234Ryan Fisher25/02/2019
1234Danny Franklin22/12/2018
0987Ryan Fisher14/02/2019
3453John Hazeldine12/01/2019
3453Jake Spencer31/12/2018

<tbody>
</tbody>

This list shows all allocations for each batch. Is it possible to have a query which shows a list of all Batches that do not have any records with a certain Salesperson's name?

E.G If the query was for Alan Smith, then batches with records with Alan Smith in the Salesperson Name field are excluded? Meaning it would return something like this: (It would return 1 record per Batch - The record with the most recent Date Allocated)

BatchSalesperson NameDate Allocated
0987Ryan Fisher14/02/2019
3453John Hazeldine12/01/2019

<tbody>
</tbody>


Very complicated explanation but if there is any advice, it would be much appreciated!
 
The last column is another field in the table.

The table fields I'll use are:
Batch, Date Allocated, Salesperson, Currently Allocated

E.g. The table will show this for batch ABC1
BatchDate AllocatedSalespersonCurrently Allocated
ABC101/12/2018James SmithYes
ABC130/09/2018Alan SmithNo
ABC114/08/2018Danny FranklinNo

<tbody>
</tbody>

I'd want the query to show:
BatchDate Allocated To AlanLast Allocated ToLast Allocated DateCurrently Allocated
ABC130/09/2018James Smith01/12/2018Yes

<tbody>
</tbody>

So the final 3 columns are based on the record with the highest Date Allocated for that specific batch and the 'Date Allocated to Alan' returns the date in the record which shows when it was allocated to him.

Does this help?
 
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
OK, though it is not always necessary, sometimes it is easier to understand if you break the queries up into different queries, and do "queries of queries". Sometimes it is hard to understand or do if you try to do too much in one single query.

So first, let's create a query called "qryAlanSmith", which returns the batches that have been allocated to Alan smith and have "Currently Allocated" set to "No" for records with his name.
The SQL code of that query might look like this:
Code:
SELECT SalesTable.Batch, Sum(IIf([Salesperson Name]="Alan Smith",1,0)) AS [Check], Max(SalesTable.[Date Allocated]) AS [Date Allocated to Alan]
FROM SalesTable
WHERE (((SalesTable.[Currently Allocated])="No"))
GROUP BY SalesTable.Batch
HAVING (((Sum(IIf([Salesperson Name]="Alan Smith",1,0)))>0));

Now, let's create a second query (and call it "qryLastAllocated", which shows when each batch was last allocated).
Code:
SELECT SalesTable.Batch, Max(SalesTable.[Date Allocated]) AS [Last Allocated Date]
FROM SalesTable
GROUP BY SalesTable.Batch;

Finally, let's create a third query which combines our first two queries with our original table. That code might look like this:
Code:
SELECT SalesTable.Batch, qryAlanSmith.[Date Allocated to Alan], SalesTable.[Salesperson Name] as [Last Allocated To], qryLastAllocated.[Last Allocated Date], SalesTable.[Currently Allocated]
FROM (qryAlanSmith INNER JOIN qryLastAllocated ON qryAlanSmith.Batch = qryLastAllocated.Batch) INNER JOIN SalesTable ON (qryLastAllocated.Batch = SalesTable.Batch) AND (qryLastAllocated.[Last Allocated Date] = SalesTable.[Date Allocated]);
When I did this on your data, it returned what you wanted.
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,806
Members
449,048
Latest member
greyangel23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top