MySQL TABLE :
numbers
+----+-----+------------+
| ID | NUM | CREATED |
+----+-----+------------+
| 1 | 11 | 2018-01-01 |
+----+-----+------------+
| 2 | 22 | 2018-02-01 |
+----+-----+------------+
| 3 | 11 | 2018-03-01 |
+----+-----+------------+
| 4 | 44 | 2018-04-01 |
+----+-----+------------+
| 6 | 44 | 2018-04-02 |
+----+-----+------------+
| 5 | 22 | 2018-05-01 |
+----+-----+------------+
In this table, ID is the Primary Key. NUM is the random value and CREATED is the Date.
My Query:
SELECT
DATE_FORMAT(CREATED,'%Y-%m') AS Month,
COUNT(NUM) AS TotalNum,
COUNT(DISTINCT(NUM)) AS UniqueNum
FROM
`numbers`
GROUP BY
DATE_FORMAT(CREATED,'%Y-%m')
Result of My Query:
+---------+----------+-----------+
| Month | TotalNum | UniqueNum |
+---------+----------+-----------+
| 2018-01 | 1 | 1 |
+---------+----------+-----------+
| 2018-02 | 1 | 1 |
+---------+----------+-----------+
| 2018-03 | 1 | 1 |
+---------+----------+-----------+
| 2018-04 | 2 | 1 |
+---------+----------+-----------+
| 2018-05 | 1 | 1 |
+---------+----------+-----------+
But my Expected Result is :
+---------+----------+-----------+
| Month | TotalNum | UniqueNum |
+---------+----------+-----------+
| 2018-01 | 1 | 1 |
+---------+----------+-----------+
| 2018-02 | 1 | 1 |
+---------+----------+-----------+
| 2018-03 | 1 | 0 |
+---------+----------+-----------+
| 2018-04 | 2 | 1 |
+---------+----------+-----------+
| 2018-05 | 1 | 0 |
+---------+----------+-----------+
Result of UniqueNum should be 0 in Month 2018-03. Because NUM 11 is already exists in Month 2018-01.
Also
Result of UniqueNum should be 0 in Month 2018-05 . Because NUM 22 already exists in Month 2018-02.
I want to find the Unique number for every Month. How can I get my expected result by updating my MySQL Query? Please help.