I'm trying to work with some logging data in MySQL where I want to be able to tell, per individual, counts of the number of distinct successful requests, distinct unsuccessful requests, non-distinct successful requests, and non-distinct unsuccessful requests that all meet additional global criteria.
Here is a sample of made up data that represents the original table:
+--------------+--------+---------------+----------+------+
| request_date | client | request | response | site |
+--------------+--------+---------------+----------+------+
| 10/30/2018 | Amy | RE_food | 200 | MD |
| 10/30/2018 | Amy | RE_food | 200 | MD |
| 10/30/2018 | Amy | RE_transport | 200 | MD |
| 10/30/2018 | Amy | RE_assistance | 404 | MD |
| 10/30/2018 | Amy | OA_assistance | 404 | MD |
| 10/30/2018 | Bob | RE_food | 200 | MD |
| 10/30/2018 | Dan | RE_food | 404 | MD |
| 10/30/2018 | Dan | RE_food | 404 | SE |
| 10/30/2018 | Mike | RE_transport | 200 | SE |
| 10/30/2018 | Sally | RE_food | 404 | SE |
| 10/30/2018 | Sally | OA_food | 404 | MD |
| 10/30/2018 | Zeb | RE_assistance | 404 | MD |
| 10/30/2018 | Zeb | OA_assistance | 404 | MD |
+--------------+--------+---------------+----------+------+
I want to write a query that will return
+--------+-----------+------------+----------+-----------+
| client | unique_OK | unique_404 | total_OK | total_404 |
+--------+-----------+------------+----------+-----------+
where unique means counting only the distinct client, request, response groupings. it would return the results
+--------+-----------+------------+----------+-----------+
| client | unique_OK | unique_404 | total_OK | total_404 |
+--------+-----------+------------+----------+-----------+
| Amy | 2 | 1 | 3 | 1 |
| Bob | 1 | 0 | 1 | 0 |
| Dan | 0 | 1 | 0 | 1 |
| Mike | 0 | 0 | 0 | 0 |
| Sally | 0 | 0 | 0 | 0 |
| Zeb | 0 | 1 | 0 | 1 |
+--------+-----------+------------+----------+-----------+
(Note that shown above I am showing the clients with 0 counts across all four columns for clarity about the process; it's fine if they are not included in the response.)
I tried queries like this that don't work, but hopefully it makes sense as to what I'm trying to do:
SELECT client AS person,
COUNT(DISTINCT request WHERE response = '200') AS unique_OK,
COUNT(DISTINCT request WHERE response = '404') AS unique_404
COUNT(WHERE response = '200') as total_OK,
COUNT(WHERE response = '404') as total_404
FROM transactions
WHERE request like '%RE%' and site = MD and DATE_FORMAT(request_date, '%Y%m%d')='20181030';