我正在尝试将多个帐户组合在一起,同时保持滚动/续订余额。使用一个帐户的查询:
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
输出
输出两个帐户:
正如您所看到的,它不会将两个帐户组合在一起..
我想要发生的事情是添加几个帐户,对财政期间/年的12行内的所有内容进行汇总。
最终,我需要: All accounts
感谢任何建议。
--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
Date_Fiscal表:Date_Fiscal
答案 0 :(得分:1)
以下是使用聚合的窗口函数的方法:
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;
请注意,您的IN
看起来很奇怪。它看起来更像是SQL Server不支持的正则表达式。
SUM(SUM())
起初看起来很奇怪,但你会很快习惯它。内部SUM()
来自聚合。外部SUM()
用于窗口函数。
答案 1 :(得分:0)
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
答案 2 :(得分:0)
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
帐号是什么?是01260,0000&amp; 000? 如果是,您的IN将是(&#39; 01260&#39;,&#39; 0000&#39;,&#39; 000&#39;) 这是未经测试的,但我从一个类似的那个拿走了它。
希望有所帮助