I have a table called tbl_points with the following columns:
[key] identity
[fkey] int -> forign key from other table
[points] int -> number
[inputdate] datetime -> getdate()
And values like:
key,fkey,points,inputdate
1,23,5,20170731
2,23,2,20170801
3,23,4,20170801
4,25,2,20170801
5,25,2,20170802
6,23,5,20170803
7,25,3,20170803
8,23,5,20170804
I am executing a query like this:
select fkey,sum(points) points,month(inputdate) mnd,year(inputdate) yy
from tbl_points
group by fkey,month(inputdate) mnd,year(inputdate)
order by year(inputdate),month(inputdate) mnd,points
Which gives as result:
fkey,points,mnd,yy
23,14,8,2017
25,7,8,2017
25,5,7,2017
So far so good. Now I want only the top 1 of each month, so
23,14,8,2017
25,5,7,2017
I can do this in the code, or in the stored procedure with a temporary table or cursor.
But perhaps there is is simpler solution. Any ideas? Or a better approach?