Hopefully I'll be able to explain this better than the title.
I have an activity table that looks like this:
|ID| |LicenseNumber| |DateTime| |1 | |123 | |2017-11-17 11:19:04.420| |2 | |123 | |2017-11-26 10:16:52.790| |3 | |123 | |2018-02-06 11:13:21.480| |4 | |123 | |2018-02-19 10:12:32.493| |5 | |123 | |2018-05-16 09:33:05.440| |6 | |123 | |2019-01-02 10:05:25.193|
What I need is a count of rows per License Number, grouped in essentially 12 month intervals. But, the year needs to start from when the previous entry ended.
For example, I need a count of all records for 12 months from 2017-11-17 11:19:04.420, and then I need a count of all records starting from (2017-11-17 11:19:04.420 + 12 months) for another 12 months, and so on.
I've considered using recursive CTEs, the LAG function etc. but can't quite figure it out. I could probably do something with a CASE statement and static values, but that would require updating the report code every year.
Any help pointing me in the right direction would be much appreciated!