sevmusic
sevmusic

Reputation: 126

MySQL Get counts in subquery without grouping

I've been trying to find a solution to having a count appear for each entry there is a duplicate. But every query I've tried either groups all the entries into one row or one count is incorrect.

Here is my table without a count:

------------------------------------------------------
| id | last_name | last_4_ssn | type | complete_date | 
------------------------------------------------------
| 1  | TEST      | 1234       | CC   | NULL          |
| 2  | TEST      | 1234       | CC   | NULL          |
| 3  | TEST      | 1234       | CC   | 2016-09-11    |
| 4  | TEST      | 1234       | CC   | 2016-09-06    |
| 5  | TEST      | 1234       | CC   | NULL          |
| 6  | TEST      | 1234       | FE   | 2016-08-30    |
| 7  | TEST      | 1234       | FE   | 2016-09-01    |
| 8  | TEST      | 1234       | FE   | 2016-09-02    |
------------------------------------------------------

I'm trying to get a count that checks 'last_name', 'last_4_ssn' and 'type' with a complete_date that is NOT NULL.

I would like my resulting table to look like this:

--------------------------------------------------------------
| id | last_name | last_4_ssn | type | complete_date | count | 
--------------------------------------------------------------
| 1  | TEST      | 1234       | CC   | NULL          | 0     |
| 2  | TEST      | 1234       | CC   | NULL          | 0     |
| 3  | TEST      | 1234       | CC   | 2016-09-11    | 2     |
| 4  | TEST      | 1234       | CC   | 2016-09-06    | 2     |
| 5  | TEST      | 1234       | CC   | NULL          | 0     |
| 6  | TEST      | 1234       | FE   | 2016-08-30    | 3     |
| 7  | TEST      | 1234       | FE   | 2016-09-01    | 3     |
| 8  | TEST      | 1234       | FE   | 2016-09-02    | 3     |
--------------------------------------------------------------

Notice how there are 2 CC with complete dates and 3 FEs with complete dates? This is the count I am trying to get; where last_name, last_4_ssn and type all equal the same and their complete_dates are NOT NULL.

The current SQL that I am trying is (but not quite there):

SELECT 
    clients.id, 
    clients.session_type, 
    clients.last_name, 
    clients.complete_date, 
    clients.last_4_ssn, 
    dups.the_count
FROM clients
LEFT JOIN (
    SELECT id,
        COUNT(*) AS the_count, 
        session_type, 
        last_4_ssn, 
        last_name, 
        complete_date
    FROM `clients`          
    GROUP BY last_name, last_4_ssn, session_type
    HAVING the_count > 1 AND complete_date IS NOT NULL ) AS dups
ON (dups.last_4_ssn = clients.last_4_ssn AND dups.last_name = clients.last_name AND dups.session_type = clients.session_type)
GROUP BY clients.id

Any ideas would be greatly appreciated. Thanks for your time!

Upvotes: 2

Views: 45

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522741

SELECT t1.*,
       COALESECE(t2.count, 0)              -- NULL dates should show a 0 count
FROM clients t1
LEFT JOIN
(
    SELECT last_name,
           last_4_ssn,
           type,
           SUM(CASE WHEN complete_date IS NOT NULL THEN 1 ELSE 0 END) AS `count`
    FROM clients
    GROUP BY last_name,
             last_4_ssn,
             type
) t2
    ON t1.last_name  = t2.last_name AND    -- join on name, ssn, and type
       t1.last_4_ssn = t2.last_4_ssn AND
       t1.type       = t2.type AND
       t1.complete_date IS NOT NULL        -- but only join the count if date not NULL

Demo here:

SQLFiddle

Upvotes: 2

Related Questions