Access JumpStart 2.0 | Blog

A Rapid Development Framework for Microsoft Access

In SQL Server you can count up the distinct values in a column of a query using syntax like:

SELECT SalesPersonID, COUNT(DISTINCT CustomerID) As NumberOfDistinctCustomersServed FROM ClosedSales
GROUP BY SalesPersonID

This will give you the number of unique customers that SalesPersonID has closed.

Access only allows you to use DISTINCT to get unique records over the whole recordset, like:

SELECT DISTINCT SalesPersonID, CustomerID FROM ClosedSales

This would give you a list of unique SalesPersonIDs paired with CustomerIDs, but you can’t count them in this step.

In Access you could use the SELECT DISTINCT query as a subquery and then count the CustomerIDs in this query to get the same results as the SQL Servier query. Here is how that would look:

SELECT SalesPersonID, COUNT(CustomerID) FROM
  (SELECT DISTINCT SalesPersonID, CustomerID FROM ClosedSales) AS SubQuery
GROUP BY SalesPersonID

This is a little more ugly and hard to plugin to other queries, but it works.

Sign up For a Daily Email Adventure in Microsoft Access

Every business day (typically M-F), I'll send you an email with information about my ongoing journey as an advanced Access application developer. It will be loaded with my tips and musings.

    We won't send you spam. Unsubscribe at any time.