sql server - How to calculate running total column which has more than one date where criteria -
i'm using sql server 2012 , have query need convert running total per month.
the current query expect input parameter of date , outputs groups total per group input month date
declare @inputdate datetime set monthenddate = '2016/07/31' select p.groupid,count(p.personid) grouptotal persondata p p.starteddate not null , p.starteddate < @inputdate , ( p.outcomedate > @inputdate or p.outcomedate null ) group p.groupid
using same query logic need repopulate historic data per month need use running total months e.g. (2016/01/01,2016/02/01,2016/03/01 etc) , not specific month
i can if there 1 date criteria e.g. starteddate e.g.
select p.*, sum(grouptotal) on (partition groupid order startedmonth) runningtotal ( select p.groupid,left(convert(varchar, p.starteddate, 112), 6) startedmonth,count(p.personid) grouptotal persondata p p.starteddate not null group p.groupid,left(convert(varchar, p.starteddate, 112), 6) ) p order groupid,startedmonth
but original query has 2 date criteria not one
1. p.starteddate < @inputdate 2. p.outcomedate > @inputdate or p.outcomedate null
is possible write query has more 1 date criteria running total
edit:
here example of input persondata table
personid,groupid,starteddate,outcomedate 1,1001,'2016/05/08',null 2,1001,'2016/05/04','2016/08/03' 3,1001,'2016/06/04','2016/08/03' 4,1001,'2016/07/04','2016/07/07' 5,1001,'2016/07/04','2016/08/08' 6,1001,'2016/08/04','2016/09/03' 7,1001,'2016/08/04','2016/09/03' 8,1001,'2016/09/04','2016/09/08'
expected output
groupid,endmonthdate,monthcount, rtmonthcount 1001,'2016/05/31', 2, 2 1001,'2016/06/30', 1, 3 1001,'2016/07/31', 1, 4 1001,'2016/08/31', 2, 6 1001,'2016/09/31', 0, 6
so in above example can see person id 4 & 8 not counted criteria no 1 matched not criteria no 2.
how below, need partition (groupid, startedmonth) calculate total each month, , running total, rows between unbounded preceding , current row)
select p.*, sum(grouptotal) on (partition groupid, startedmonth) monthtotal, sum(grouptotal) on (partition groupid order startedmonth rows between unbounded preceding , current row) runningtotal ( select p.groupid,left(convert(varchar, p.starteddate, 112), 6) startedmonth,count(p.personid) grouptotal persondata p p.starteddate not null group p.groupid,left(convert(varchar, p.starteddate, 112), 6) ) p order groupid,startedmonth
check out link also: calculate running total in sql server
** second version, added condition subquery
select p.*, sum(grouptotal) on (partition groupid, startedmonth) monthtotal, sum(grouptotal) on (partition groupid order startedmonth rows between unbounded preceding , current row) runningtotal ( select p.groupid,left(convert(varchar, p.starteddate, 112), 6) startedmonth,count(p.personid) grouptotal persondata p p.starteddate not null , (p.outcomedate null or (p.outcomedate> eomonth(p.starteddate))) group p.groupid,left(convert(varchar, p.starteddate, 112), 6) ) p order groupid,startedmonth
Comments
Post a Comment