I have two tables, T1 AND T2.
T1 with following columns: Id, TypeofValue, Year, value
Typeofvalue
can have 2 values
1
- indicates Actual
2
- indicates Target
T2 With following column: NoOfRecordsToDisplay
I need to fetch the number of records (if existing) for Target
corresponding to an Id
.
However, the catches are:
- Sometimes Target value might not be present for a year
- I need to get only last records for targets on the basis of NoOfRecordsToDisplay (The number of records to display comes from T2) for actual
Example1: NoOfRecordsToDisplay =3, ID =123 The data below should return 3 as we have 3 non null values for target for last 3 years -2015, 2014,2013 in this case
Id TypeofValue Year Value
123 1 2015 55
123 1 2014 56
123 1 2013 57
123 1 2012 58
123 2 2015 50
123 2 2014 50
123 2 2013 50
123 2 2012 50
124 1 2015 55
124 1 2014 56
124 1 2013 57
124 1 2012 58
124 2 2015 50
124 2 2014 50
124 2 2013 50
124 2 2012 50
Another dataset - NoOfRecordsToDisplay =3, ID =123 The data below should return 0, as we have no values for target for last 3 years -2015, 2014,2013
Id TypeofValue Year Value
123 1 2015 55
123 1 2014 56
123 1 2013 57
123 1 2012 58
123 2 2012 50
124 1 2015 55
124 1 2014 56
124 1 2013 57
124 1 2012 58
124 2 2012 50