Mysql – Count total unique rows for each date and group by date

MySQLquerysubquery

I have the following table:

| email   | date_added |
-------------------------
| a@a.com | 2019-01-01 |
| b@b.com | 2019-01-01 |
| a@a.com | 2019-01-02 |
| c@c.com | 2019-01-02 |
| x@x.com | 2019-01-02 |
| a@a.com | 2019-01-03 |

I am looking for the following result

| count_unique_emails | date       |
|         2           | 2019-01-01 |
|         4           | 2019-01-02 |
|         4           | 2019-01-03 |

I have no idea where to start from.
Any thoughts?

Thanks

Best Answer

SELECT COUNT(DISTINCT srctable.email), dates.date_added
FROM srctable
JOIN ( SELECT DISTINCT date_added 
       FROM srctable ) dates ON srctable.date_added <= dates.date_added
GROUP BY dates.date_added