I have a table 1 of data that looks a bit like that:
Record# Date Person
1 12/12/2012 Tom
2 01/02/2013 Tom
3 10/02/2013 Tom
4 02/01/2013 John
5 04/01/2014 John
6 30/06/2010 Mary
7 30/06/2011 Mary
8 30/06/2012 Mary
9 30/07/2012 Mary
and another table 2 where we have the registration date of each Person
Person RegisterDate MaxRecord
Tom 15/12/2011 100
John 01/01/2013 10
Mary 16/06/2010 50
Before adding a record in the table1, I need to check whether the annual count of record (table1) by Person is lower than the MaxRecord number (table2) for that Person. By Annual, I mean startDate = registration date and endDate = registration date + 1 year and not from Januray 1st till December 31st.
If I want to add a record for Mary, I want to write SQL that will give me the following output:
StartDate EndDate CountRecord
16/06/2010 15/06/2011 1
16/06/2011 15/06/2012 1
16/06/2012 15/06/2013 2
Once this output is build, I could test whether thedate of a new record (for a Person) is allowed or not.
Could someone give me a clue, a link to a tutorial or some help please?