I have a data like below
+------+---------+--------------+
| ID | Version | CreationDate |
+------+---------+--------------+
| 8103 | 1 | 20-Nov-2020 |
+------+---------+--------------+
| 8103 | 2 | 30-Mar-2021 |
+------+---------+--------------+
| 8103 | C | 04-Jun-2021 |
+------+---------+--------------+
| 7546 | A | 05-May-2020 |
+------+---------+--------------+
| 7546 | 1 | 08-Apr-2021 |
+------+---------+--------------+
| 7546 | 2 | 02-Sep-2021 |
+------+---------+--------------+
| 4233 | 1 | 01-Jun-2019 |
+------+---------+--------------+
| 4233 | 2 | 01-Feb-2020 |
+------+---------+--------------+
| 6626 | 1 | 14-Jul-2020 |
+------+---------+--------------+
Required Output.
+------+--------+
| ID | Count |
+------+--------+
| 8103 | 2 |
+------+--------+
| 7546 | 1 |
+------+--------+
| 4233 | IGNORE |
+------+--------+
| 6626 | 0 |
+------+--------+
Logic:
- IDs first version should be created after 2020. If it is created before 2020, then ignore. E.g 4233 in my sample data
- Count the number of version after the initial version within one year after it gets created. If we see the ID 7546, the version-
2is created after one year of versionA. But I need to find how many versions were created within a year of its first version.
My Try:
SELECT ID,SUM(CASE WHEN (year(creation_date)-MIN(year(creation_date)))=0 THEN 1 END) as CNT
FROM MYTABLE
WHERE year(creation_date)>2019
GROUP BY ID
I'm really stuck getting how to get that count. Please help