如何重置月累计 运行 总数

How to reset the month to date running total

SELECT DISTINCT
    ACCOUNTDATE,
    PROPERTYNAME,
    rt.management
FROM 
    aaa t
CROSS APPLY
    (SELECT 
         SUM(MANAGEMENT) AS management
     FROM
         aaa
     WHERE
         PROPERTYNAME = t.PROPERTYNAME 
         AND ACCOUNTDATE <= t.ACCOUNTDATE) as rt
WHERE 
    AccountDate BETWEEN @STARTOFMONTH_MAN AND @ENDOFMONTH_MAN
ORDER BY 
    AccountDate

这是我的查询,我想计算本月至今的 运行 总数, 例如:1/08/2016-31/08/2016 的 运行 总数,并且必须从 1/09/2016-31/09/2016.

的开头重新设置总数

但是根据上面的查询,我无法重置下个月的总数。

如果您想要 运行 总数,那么在 SQL Server 2012+ 中,您将使用:

select aaa.*,
       sum(management) over (partition by year(accountdate), month(accountdate)
                             order by accountdate
                            ) as rt
from aaa
where AccountDate between @STARTOFMONTH_MAN and @ENDOFMONTH_MAN
order by Accountdate;

在早期版本中,您可以使用 cross apply 并在逻辑中包括年份和月份:

SELECT t.*, rt.management
from aaa t cross apply     
     (select SUM(MANAGEMENT) as management
      from aaa t2
      where t2.PROPERTYNAME = t.PROPERTYNAME and
            year(t2.accountdate) = year(t.accountdate) and
            month(t2.accountdate) = month(t.accountdate) and
            t2.ACCOUNTDATE <= t.ACCOUNTDATE 
      ) rt
WHERE AccountDate BETWEEN @STARTOFMONTH_MAN AND @ENDOFMONTH_MAN
ORDER BY AccountDate;

如果您不使用 SQL-Server 2012+ ,则必须使用相关查询(或某种其他连接)来完成它:

SELECT DISTINCT t.ACCOUNTDATE
       ,t.PROPERTYNAME
       ,(SELECT SUM(s.MANAGMENT) FROM aaa s
         WHERE MONTH(t.accountdate) = MONTH(s.accountdate)
           AND YEAR(t.accountdate) = YEAR(s.accountdate)
           AND  s.PROPERTYNAME = t.PROPERTYNAME) as rt
from aaa t
WHERE t.AccountDate BETWEEN @STARTOFMONTH_MAN AND @ENDOFMONTH_MAN
ORDER BY t.AccountDate

尝试

SELECT DISTINCT
    ACCOUNTDATE
    ,PROPERTYNAME
    ,rt.management
from aaa t
cross apply     
    (select SUM(MANAGEMENT) as management
     from aaa
     where 
     PROPERTYNAME = t.PROPERTYNAME and
     ACCOUNTDATE BETWEEN dateadd(MONTH, datediff(MONTH, 0,t.ACCOUNTDATE),0) -- start of month
AND t.ACCOUNTDATE 
    ) as rt
WHERE AccountDate BETWEEN @STARTOFMONTH_MAN AND @ENDOFMONTH_MAN
ORDER BY AccountDate
SELECT DISTINCT
    ACCOUNTDATE
    ,PROPERTYNAME
    ,rt.management
from aaa t
cross apply     
    (select SUM(MANAGEMENT) as management
     from aaa
     where 
     PROPERTYNAME = t.PROPERTYNAME and
     ACCOUNTDATE BETWEEN dateadd(MONTH, datediff(MONTH, 0,t.ACCOUNTDATE),0) -- start of month
AND t.ACCOUNTDATE 
    ) as rt
WHERE AccountDate BETWEEN @STARTOFMONTH_MAN AND @ENDOFMONTH_MAN
ORDER BY AccountDate

为了始终给出项目所在月份的 运行 总计,试试这个 - 它避免对任何前几个月的帐户求和

    SELECT DISTINCT
    ACCOUNTDATE,
    PROPERTYNAME,
    rt.management
FROM 
    aaa t
CROSS APPLY
    (SELECT 
         SUM(MANAGEMENT) AS management
     FROM
         aaa
     WHERE
         PROPERTYNAME = t.PROPERTYNAME 
         AND ACCOUNTDATE <= t.ACCOUNTDATE 
         and MONTH(accountdate) = MONTH(t.accountdate) 
         and year(accountdate) = year(t.accountdate)) as rt
WHERE 
    AccountDate BETWEEN @STARTOFMONTH_MAN AND @ENDOFMONTH_MAN
ORDER BY 
    AccountDate

从 4 月开始的财政年度试试这个

    SELECT DISTINCT
    ACCOUNTDATE,
    PROPERTYNAME,
    rt.management,
    fy.managementFinancialYear
FROM 
    aaa t
CROSS APPLY
    (SELECT 
         SUM(MANAGEMENT) AS management
     FROM
         aaa
     WHERE
         PROPERTYNAME = t.PROPERTYNAME 
         AND ACCOUNTDATE <= t.ACCOUNTDATE 
         and MONTH(accountdate) = MONTH(t.accountdate) 
         and year(accountdate) = year(t.accountdate)) as rt
CROSS APPLY
    (SELECT 
         SUM(MANAGEMENT) AS managementFinancialYear
     FROM
         aaa
     WHERE
         PROPERTYNAME = t.PROPERTYNAME 
         AND ACCOUNTDATE <= t.ACCOUNTDATE 
         and (year(accountdate) * 12 + MONTH(accountdate) - 4) % 12  = (year(t.accountdate) * 12 + MONTH(t.accountdate) - 4) % 12 
         )as fy
WHERE 
    AccountDate BETWEEN @STARTOFMONTH_MAN AND @ENDOFMONTH_MAN
ORDER BY 
    AccountDate