如何重置月份运行总计

时间:2016-08-10 10:58:57

标签: sql-server sql-server-2008 tsql business-intelligence

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开始重置。

但是从上面的查询来看,我无法重置下个月的总数。

5 个答案:

答案 0 :(得分:1)

如果您想要运行总计,那么在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;

答案 1 :(得分:1)

如果您不使用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

答案 2 :(得分:0)

尝试

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

答案 3 :(得分:0)

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

答案 4 :(得分:0)

为了始终为该项目所在的月份提供运行总计,请尝试此操作 - 它可以避免对前几个月的帐户进行汇总

psql -h localhost -d userstoreis -U admin -p 5432 -a -q -f /home/jobs/Desktop/resources/postgresql.sql

-h PostgreSQL server IP address
-d database name
-U user name
-p port which PostgreSQL server is listening on
-f path to SQL script
-a all echo
-q quiet 
-f file

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

    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