简体   繁体   中英

Counting Unique Customers in Categories Based on Call Log Status Hierarchy

I have a table of call logs of customers. For the purposes of what I'm trying to do, the table only consists of Customer ID and Call Status. Each customer could have multiple entries with varied call statuses.

Eg table:

Customer    Status
1           Didn't Reach - No Voicemail
3           Left a Voicemail
2           Left a Voicemail
1           Left a Voicemail
1           Talked With Customer
3           Didn't Reach - No Voicemail
2           Talked With Customer
2           Left a Voicemail
2           Talked With Customer

My Output should be:

Status                      # of Customers
Talked With Customer        2
Left a Voicemail            1
Didn't Reach - No Voicemail 0

This is due to the fact that from the 3 different status their heirarchy is as follows:

1. Talked with Customer
2. Left a Voicemail
3. Didn't Reach - No Voicemail

A patient should only be counted in the highest category (1>2>3), which is why in my example, there are no customers in "Didn't reach - no voicemail" as all of them had AT LEAST a voicemail left.

Hopefully this makes sense and thanks in advance!

An SQL statement can produce this output. Does require a table of status hierarchy if want to show all status even if there is no data for a status.

Can export query to Excel or Excel can link to query object. Or Excel VBA can connect to Access db and pull data with SQL statement to a recordset object and use CopyFromRecordset method to save data to worksheet range.

SELECT StatusHierarchy.StatusDesc, Count(Query1.Customer) AS CntStat
FROM (SELECT Customer, Min(StatusValue) AS MinOfStatusValue
      FROM StatusHierarchy 
      LEFT JOIN Table1 ON StatusHierarchy.StatusDesc = Table1.Status
      GROUP BY Table1.Customer) AS Query1 
RIGHT JOIN StatusHierarchy ON Query1.MinOfStatusValue = StatusHierarchy.StatusValue
GROUP BY StatusHierarchy.StatusDesc
ORDER BY Count(Query1.Customer) DESC;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM