getting max value from running total by month

I have written the following query in sql server 2017 which gives me the running total of a field called numberchecked for day, month, and year and this works perfectly.  I now need the max total, by month, but am a little bit brain fogged on how to get this.  My query is below.  What do I need to add?

Table has 2 fields:
    date datetime,
    numberchecked int

select date,
		sum(numberchecked) over (partition by date order by date) as rt,
		sum(numberchecked) over (partition by datepart(month, date) order by date) as rtm,
		sum(numberchecked) over (partition by datepart(year, date) order by date) as rty
from Checked
where datepart(year, date) = 2017

Open in new window

dan hendersonAsked:
Who is Participating?
Olaf DoschkeSoftware DeveloperCommented:
I assume you'd also want to get rid of all the days not at the end of the month, so just first sum grouped by month, then do the running total:

With monthly as(
select max(date) as lastdate, 
       sum(numberchecked) as tm
	    from Checked
where    datepart( year, date) = 2017
group by datepart(month, date)
Select lastdate,
	sum(tm) over (partition by datepart(month, lastdate) order by lastdate) as rtm,
	sum(tm) over (partition by datepart( year, lastdate) order by lastdate) as rty
from monthly

Open in new window

Bye, Olaf.
Kyle AbrahamsSenior .Net DeveloperCommented:
is it just as easy as:
max(numberchecked) over (partition by datepart(month, date) ) as rtmAx,

Open in new window

Don't have 2017 to check it out.
dan hendersonAuthor Commented:
thanks.  I was having a brain cramp there.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.