简体   繁体   English

滚动总和-年/月/月

[英]Rolling Sum - Year by Year/Month By Month

I'm attempting to group several accounts together while keep a rolling / renewing balance. 我试图将多个帐户分组在一起,同时保持滚动/续订余额。 The query using one account: 使用一个帐户查询:

SELECT FiscalPeriod, BalanceAmt,
       SUM(balanceamt) OVER(ORDER BY fiscalperiod) as BalanceAmt
FROM EpicorLive10.Erp.GLPeriodBal
WHERE FiscalYear = '2018'
AND BalanceAcct IN ('01260|0000|000')
GROUP BY fiscalperiod,BalanceAmt
ORDER BY FiscalPeriod

OUTPUT OUTPUT

输出1

With two accounts output: 有两个帐户输出:

输出2

As you can see, it doesn't group both account together.. 如您所见,它不会将两个帐户组合在一起。

What I want to happen is to add several accounts, sum everything / group everything within the 12 rows of fiscal period / year. 我想做的是添加几个帐户,对所有会计年度/年度的12行中的所有事项进行汇总/分组。

Ultimately, I'd need: All accounts 最终,我需要: 所有帐户

Any advice is appreciated. 任何建议表示赞赏。

--UPDATE** Current Query**  
select Datex, balanceamt into #temp 
FROM EpicorLive10.Erp.GLPeriodBal as A  
JOIN EpicorLive10.dbo.Date_Fiscal as B  
ON a.FiscalPeriod = b.Fiscal_Period and a.FiscalYear = b.Fiscal_Year   
WHERE FiscalYear = '2018'  
AND BalanceAcct IN (   
'01260|0000|000',  
'01261|0000|000',  
'01262|0000|000',  
'01263|0000|000',  
'01264|0000|000',  
'01555|0000|000',  
'01560|0000|000',  
'01245|0000|000',  
'01250|0000|000')  
GROUP BY Datex,BalanceAmt  


declare @rollup table (rollupid int identity, fiscalperiod int,     balanceamt float)  
insert @rollup  
select Datex, sum(balanceamt) from #temp  
group by Datex  
order by Datex  


declare @holding table (fiscalperiod int, balanceamt float, runningbal     float)  
declare @iterator int=0  
declare @currentbal float =0  
while @iterator<=(select max(rollupid) from @rollup) begin  
select @currentbal=@currentbal+balanceamt from @rollup where       fiscalperiod=@iterator  
insert @holding  
select fiscalperiod, balanceamt, @currentbal from  
@rollup where  
fiscalperiod=@iterator  
 set @iterator=@iterator+1  
 end  

select * from @holding  

drop table #temp  

Account Table: Account Table 帐户表: 帐户表
Date_Fiscal Table: Date_Fiscal Date_Fiscal表: Date_Fiscal

Here is how you use window functions with aggregation: 这是将窗口函数用于聚合的方法:

SELECT FiscalPeriod, BalanceAmt,
       SUM(SUM(balanceamt)) OVER (ORDER BY fiscalperiod) as BalanceAmt
FROM EpicorLive10.Erp.GLPeriodBal
WHERE FiscalYear = '2018' AND BalanceAcct IN ('01260|0000|000')
GROUP BY fiscalperiod
ORDER BY FiscalPeriod;

Note that your IN looks strange. 请注意,您的IN看起来很奇怪。 It looks more like regular expression, which SQL Server does not support. 它看起来更像是SQL Server不支持的正则表达式。

The SUM(SUM()) looks strange at first, but you'll get used to it quickly enough. 首先, SUM(SUM())看起来很奇怪,但是您会很快习惯它。 The inner SUM() is from the aggregation. 内部的SUM()来自聚合。 The outer SUM() is for the window function. 外部SUM()用于窗口函数。

select Datex, balanceamt into #temp 
FROM EpicorLive10.Erp.GLPeriodBal  A  
JOIN EpicorLive10.dbo.Date_Fiscal  B  
ON a.FiscalPeriod = b.Fiscal_Period and a.FiscalYear = b.Fiscal_Year   
WHERE FiscalYear = '2018'  
AND BalanceAcct IN (   
'01260|0000|000',  
'01261|0000|000',  
'01262|0000|000',  
'01263|0000|000',  
'01264|0000|000',  
'01555|0000|000',  
'01560|0000|000',  
'01245|0000|000',  
'01250|0000|000')  
GROUP BY Datex, BalanceAmt  


declare @rollup table (rollupid int identity, Datex datetime, balanceamt float)  
insert @rollup  
select Datex, sum(balanceamt) from #temp  
group by Datex  
order by Datex  


declare @holding table (Datex datetime, balanceamt float, runningbal float)  
declare @iterator int=1  
declare @currentbal float =0  
while @iterator<=(select max(rollupid) from @rollup) begin  
select @currentbal=@currentbal+balanceamt from @rollup where @iterator=rollupid   
insert @holding  
select Datex, balanceamt, @currentbal from  
@rollup where @iterator=rollupid     
 set @iterator=@iterator+1  
 end  

select * from @holding  

drop table #temp 
SELECT FiscalPeriod, Sum(BalanceAmt),
       SUM(balanceamt) OVER(ORDER BY fiscalperiod  ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as BalanceAmt
FROM EpicorLive10.Erp.GLPeriodBal
WHERE FiscalYear = '2018'
AND BalanceAcct IN ('01260|0000|000')
GROUP BY fiscalperiod,BalanceAmt
ORDER BY FiscalPeriod

What are the accounts? 有什么帐户? Are thet 01260, 0000 & 000? 是01260、0000和000吗? If yes your IN would be ('01260', '0000', '000') This is untested but I took it from a similar one I have. 如果是,则您的IN将为('01260','0000','000')这未经测试,但我从与我相似的IN中获得。

Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM