I am unsure if something like this helps?
the following code looks for the distinct manager id's then marries them up with the distinct account id's that are found in the ValidAccounts table, then joins the accToCompany field as this is a FK:
SELECT DISTINCT Customer.ManagerId, Company.AccountId, Count(1) as "Dist Count"
FROM ValidAccounts
INNER JOIN accToCompany ON ValidAccounts.Id = acctoCompany.accId
INNER JOIN Customer ON accToCompany.ManagerId = Customer.ManagerId
GROUP BY Customer.ManagerId, acctoCompany.accId ORDER BY Count(1) DESC
This gives me something like:
ManagerId | AccId | DistCount
FoxB 4017 57
Not sure if this is along the lines of what you are looking for but it what it does is query multiple tables with a goal in mind in this case finding distinct values and matching the manager and account ids.
Psymon25