年初至今 运行 财政年度总计

year to date running total for fiscal year

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

这是查找月初至今的查询。 如何从同一查询中查找财政年度的年初至今? 例如:运行 2015 年 1 月 4 日至 2016 年 3 月 31 日的总计

不确定您需要从哪一天开始 FY。假设来自@STARTOFMONTH_MAN。然后你可以开始 FY as

declare @fymonth int = 4; -- first month of FY.

declare @STARTOFMONTH_MAN date = '20160320';
select  fyStart = dateadd(MONTH,
                @fymonth - CASE WHEN month(@STARTOFMONTH_MAN) >= @fymonth THEN 1 ELSE 13 END,
                dateadd(YEAR, datediff(YEAR, 0, @STARTOFMONTH_MAN),0));

您可以通过创建日历来简化这些日期的计算 table。

编辑
想法是用较大的间隔限制 WHERE 中的数据,并有条件地对子间隔的 SUM 数据进行限制。

declare @fymonth int = 4; -- first month of FY.

SELECT DISTINCT
    ACCOUNTDATE
    ,PROPERTYNAME
    ,rt.FYManagement, rt.MonthManagement
FROM aaa t
CROSS APPLY     
    (SELECT 
        SUM(t2.MANAGEMENT) AS FYManagement
        ,SUM(CASE WHEN t2.ACCOUNTDATE BETWEEN 
                -- start of month for t.ACCOUNTDATE
            dateadd(MONTH, datediff(MONTH, 0, t3.ACCOUNTDATE), 0)
            AND t3.ACCOUNTDATE
         THEN t2.MANAGEMENT END) AS MonthManagement
     from aaa t2
     JOIN aaa t3 ON t3.primarykey = t.primarykey -- change as needed to get 1 to 1 JOIN
     where 
     t2.PROPERTYNAME = t.PROPERTYNAME and
     t2.ACCOUNTDATE BETWEEN 
                -- FY start for t.ACCOUNTDATE
                dateadd(MONTH,
                  @fymonth - CASE WHEN month(t.ACCOUNTDATE) >= @fymonth THEN 1 ELSE 13 END,
                  dateadd(YEAR, datediff(YEAR, 0, t.ACCOUNTDATE), 0));
                AND t.ACCOUNTDATE 
    ) as rt
WHERE AccountDate BETWEEN @STARTOFMONTH_MAN AND @ENDOFMONTH_MAN
ORDER BY AccountDate

声明@fymonth int = 4; -- FY 的第一个月。

SELECT 不同 帐户日期 ,财产名称 ,rt.FYManagement,rt.MonthManagement 来自 aaat 交叉应用
(SELECT SUM(t2.MANAGEMENT) 作为 FYManagement ,SUM(CASE WHEN t2.ACCOUNTDATE Between -- t.ACCOUNTDATE 月初 dateadd(MONTH, datediff(MONTH, 0, t3.ACCOUNTDATE), 0) 和 t3.ACCOUNTDATE THEN t2.MANAGEMENT END) AS MonthManagement 来自aaa t2 JOIN aaa t3 ON t3.primarykey = t.primarykey -- 根据需要进行更改以获得 1 对 1 JOIN 在哪里 t2.PROPERTYNAME = t.PROPERTYNAME 和 t2.ACCOUNTDATE介于 -- t.ACCOUNTDATE 财年开始 日期添加(月, @fymonth - CASE WHEN month(t.ACCOUNTDATE) >= @fymonth THEN 1 ELSE 13 END, dateadd(YEAR, datediff(YEAR, 0, t.ACCOUNTDATE), 0)); 和 t.ACCOUNTDATE ) 作为 rt WHERE 帐户日期在@STARTOFMONTH_MAN 和@ENDOFMONTH_MAN 之间 按账户日期排序