Hi Ryan,
Thanks for contacting us. It seems like you're encountering an issue with counting distinct states for each customer in a Microsoft Access query. The problem arises from the fact that you're getting a count of all states for each record instead of a count of distinct states per customer. To solve this, you can use a combination of SQL functions to achieve the desired result. Here's how you can approach it:
- Grouping and Counting Distinct States per Customer:
SELECT CustomerName, COUNT(DISTINCT ShippingState) AS NumberOfStates FROM Sales GROUP BY CustomerName;
This SQL query groups the sales records by the "CustomerName" column and then counts the distinct shipping states for each customer.
- Sorting by Number of States:
If you also want to sort the results by the number of states in descending order, you can add an ORDER BY clause:
SELECT CustomerName, COUNT(DISTINCT ShippingState) AS NumberOfStates FROM Sales GROUP BY CustomerName ORDER BY COUNT(DISTINCT ShippingState) DESC;
This query should give you a list of distinct customers along with the count of different states they've shipped to, sorted by the number of states in descending order.
I hope this helps.
Regards, Sola