Results 1 to 5 of 5
  1. #1
    Gardinia is offline Novice
    Windows 11 Office 365
    Join Date
    Oct 2023
    Posts
    2

    Query to find customer accounts from one salesman and exclude accounts with multiple salesmen

    Hi,

    I'm trying to query some specific data on our customer database.

    For some context our customers could have had sales contracts with multiple different sales reps over the years.

    I want to find all the customer accounts of a particular sales rep, however I then want to exclude any accounts where this sales rep and any other sales rep has sold to the same customer.

    This is where I'm unsure how to proceed, I can find everything that sales rep has sold (rep id 2) with the query below:



    Click image for larger version. 

Name:	data.jpg 
Views:	17 
Size:	121.2 KB 
ID:	50854

    However I'm not sure how I would go about expanding that to exclude any accounts (ACNo) that also have contracts (ContractNo) from another sales rep (RepNo)

    I'd be grateful for any advice on how to go about doing this.

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940


    I want to find all the customer accounts of a particular sales rep, however I then want to exclude any accounts where this sales rep and any other sales rep has sold to the same customer.
    Are you not contradicting yourself there?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    do you mean find all the customers who have had only 1 sales rep?

  4. #4
    xps35's Avatar
    xps35 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jun 2022
    Location
    Schiedam, NL
    Posts
    232
    Change your WHERE clause to
    Code:
    WHERE ContractHeader.RepNo=2 AND 
    Customers.ACNo In (SELECT Customers.ACNo FROM SalesReps 
    INNER JOIN (Customers 
    INNER JOIN ContractHeader 
    ON Customers.ACNo = ContractHeader.[ACNo]) 
    ON SalesReps.RepNo = ContractHeader.[RepNo] 
    GROUP BY Customers.ACNo 
    HAVING Count(SalesReps.RepNo)=1)
    Groeten,

    Peter

  5. #5
    Gardinia is offline Novice
    Windows 11 Office 365
    Join Date
    Oct 2023
    Posts
    2
    Quote Originally Posted by Welshgasman View Post
    Are you not contradicting yourself there?



    Quote Originally Posted by CJ_London View Post
    do you mean find all the customers who have had only 1 sales rep?
    I probably could have worded that a bit better, essentially I was looking for customers that have only ever had dealings with a single sales rep, excluding any who had dealings with multiple sales reps. So I could then narrow these customers down to a particular sales rep.





    Quote Originally Posted by xps35 View Post
    Change your WHERE clause to
    Code:
    WHERE ContractHeader.RepNo=2 AND
    Customers.ACNo In (SELECT Customers.ACNo FROM SalesReps
    INNER JOIN (Customers
    INNER JOIN ContractHeader
    ON Customers.ACNo = ContractHeader.[ACNo])
    ON SalesReps.RepNo = ContractHeader.[RepNo]
    GROUP BY Customers.ACNo
    HAVING Count(SalesReps.RepNo)=1)
    Perfect, thank you! that looks to have given me the exact data I was hoping to obtain.

    It's been a long time since I've done anything with Access / SQL queries, so I appreciate the help.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 01-16-2017, 10:42 PM
  2. Replies: 12
    Last Post: 04-20-2015, 06:44 PM
  3. Replies: 1
    Last Post: 02-23-2015, 01:19 AM
  4. How to create an accounts table?
    By Gman11 in forum Access
    Replies: 5
    Last Post: 10-12-2011, 06:43 AM
  5. profile each user accounts?
    By jun90 in forum Access
    Replies: 1
    Last Post: 01-18-2010, 03:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums