I have a table that has all eventIDs and event dates. I need to get the next two dates, beyond the current date, for a specific subset of eventIDs. I have tried many iterations and looked at many posts that return the top N results by GROUP - but I can't get any of those examples to work as those examples aren't using dates like I need to. (ex: Using LIMIT within GROUP BY to get N results per group?)
+------------+-----+
| eDate | ID |
+------------+-----+
| 2021-10-27 | 1 |
+------------+-----+
| 2021-11-03 | 1 |
+------------+-----+
| 2021-11-10 | 1 |
+------------+-----+
| 2021-11-17 | 1 |
+------------+-----+
| 2021-11-24 | 1 |
+------------+-----+
| 2021-12-01 | 1 |
+------------+-----+
| 2021-12-08 | 1 |
+------------+-----+
| 2021-10-27 | 2 |
+------------+-----+
| 2021-11-03 | 2 |
+------------+-----+
| 2021-11-10 | 2 |
+------------+-----+
| 2021-11-17 | 2 |
+------------+-----+
| 2021-11-24 | 2 |
+------------+-----+
| 2021-12-01 | 2 |
+------------+-----+
| 2021-12-08 | 2 |
+------------+-----+
| 2021-10-27 | 3 |
+------------+-----+
| 2021-11-03 | 3 |
+------------+-----+
| 2021-11-10 | 3 |
+------------+-----+
| 2021-11-17 | 3 |
+------------+-----+
| 2021-11-24 | 3 |
+------------+-----+
| 2021-12-01 | 3 |
+------------+-----+
| 2021-12-08 | 3 |
+------------+-----+
SELECT eDate,ID,COUNT(*) as eCount FROM myTable WHERE ID in (1,3) AND eDate >=CURDATE() GROUP BY ID,eDate HAVING eCount < 3 ;
This query doesn't work because the GROUP BY ID,eDate
creates a unique pair, so its returning ALL entries for IDs 1 and 3 - all with a eCount = 1
Technically I don't actually need the eCount
but it illustrates one of my many attempts to get the correct results. If today was 2020-10-28, my desired results would be following.
+------------+-----+--------+
| eDate | ID | eCount |
+------------+-----+--------+
| 2021-11-03 | 1 | 1 |
+------------+-----+--------+
| 2021-11-10 | 1 | 2 |
+------------+-----+--------+
| 2021-11-03 | 3 | 1 |
+------------+-----+--------+
| 2021-11-10 | 3 | 2 |
+------------+-----+--------+