2016-05-23 133 views
-1

從表開始;計算SQL中的平均每日餘額

[AccountLedger] 

AccountCode PostingDate   Amount  
128   2014-01-01 08:36:09 200.00 
128   2014-01-01 14:18:10 200.00 
128   2014-01-01 14:26:56  0.00 
128   2014-01-01 18:17:31 400.00 
128   2014-01-01 20:18:53 100.00 
128   2014-01-02 00:10:35  0.00 
128   2014-01-02 01:44:26 300.00 
128   2014-01-02 15:49:31 -300.00 
128   2014-01-03 00:33:23 400.00 
128   2014-01-03 11:55:13 -200.00 
128   2014-01-03 11:56:34 -100.00 
128   2014-01-03 14:58:42 -400.00  
128   2014-01-03 17:31:11  0.00 

**REQUIRED RESULT** 

AccountCode PostingDate   daily_balance 
128   2014-01-01   900.00 
128   2014-01-02   900.00 
128   2014-01-03   600.00 

查詢被附加

select 
     Acc 
    , Dte 
    , sum(daily_amt) over (PARTITION BY Acc ORDER BY Dte DESC) as daily_balance 
from (select 
      [AccountLedger].[AccountCode] as Acc 
      , convert(date, [AccountLedger].[PostingDate]) as Dte 
      , sum([AccountLedger].[Amount]) as daily_amt 
     from [AccountLedger] 
     WHERE [AccountLedger].[PostingDate] < '2014-04-01' 
     and [AccountLedger].[AccountCode]=128 

     group by [AccountLedger].AccountCode 
     , [AccountLedger].[PostingDate] 

    ) t 
     order by Acc, dte* 

*但出現錯誤;

Msg 102, Level 15, State 1, Line 4 
Incorrect syntax near 'order'. 
Msg 102, Level 15, State 1, Line 16 
Incorrect syntax near 't'.* 

我該如何得到需要的結果?

+0

什麼是爲了通過ACC,DTE *?什麼是dte *?在這種情況下,我相信錯誤消息說明了數量......並且相當準確。 – JonH

+0

這篇文章沒有顯示研究工作。 – dfundako

+0

Dte正在發佈日期 在此處的文本編輯器中格式化時,*符號會被嚴重附加。 問題來自over子句;它工作正常,直到「PARTITION BY Acc」和錯誤出現「ORDER BY DTE DESC」 – 3439027

回答

1

用一個簡單的日期表(或在我的情況下,UDF生成日期範圍)

Declare @DateR1 Date,@DateR2 Date 
Set @DateR1 = '2014-01-01' 
Set @DateR2 = '2014-01-31' 


Select AccountCode 
     ,PostingDate=Date2 
     ,DailyBalance=sum(amount) 
From #Temp A 
Join (Select [email protected],Date2=cast(RetVal as Date) from [dbo].[udf-Create-Range-Date](@DateR1,@DateR2,'DD',1)) B on cast(PostingDate as Date) between Date1 and Date2 
Group By AccountCode,Date2 
Order By 1 

返回

AccountCode PostingDate DailyBalance 
128   2014-01-01 900 
128   2014-01-02 900 
128   2014-01-03 600 
128   2014-01-04 600 
128   2014-01-05 600 
128   2014-01-06 600 
... 
128   2014-01-31 600 

的UDF

SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 

CREATE FUNCTION [dbo].[udf-Create-Range-Date] (@DateFrom datetime,@DateTo datetime,@DatePart varchar(10),@Incr int) 

Returns 
@ReturnVal Table (RetVal datetime) 

As 
Begin 
    With DateTable As (
     Select DateFrom = @DateFrom 
     Union All 
     Select Case @DatePart 
       When 'YY' then DateAdd(YY, @Incr, df.dateFrom) 
       When 'QQ' then DateAdd(QQ, @Incr, df.dateFrom) 
       When 'MM' then DateAdd(MM, @Incr, df.dateFrom) 
       When 'WK' then DateAdd(WK, @Incr, df.dateFrom) 
       When 'DD' then DateAdd(DD, @Incr, df.dateFrom) 
       When 'HH' then DateAdd(HH, @Incr, df.dateFrom) 
       When 'MI' then DateAdd(MI, @Incr, df.dateFrom) 
       When 'SS' then DateAdd(SS, @Incr, df.dateFrom) 
       End 
     From DateTable DF 
     Where DF.DateFrom < @DateTo 
    ) 

    Insert into @ReturnVal(RetVal) Select DateFrom From DateTable option (maxrecursion 32767) 

    Return 
End 

-- Syntax Select * from [dbo].[udf-Create-Range-Date]('2016-10-01','2020-10-01','YY',1) 
-- Syntax Select * from [dbo].[udf-Create-Range-Date]('2016-10-01','2020-10-01','DD',1) 
-- Syntax Select * from [dbo].[udf-Create-Range-Date]('2016-10-01','2016-10-31','MI',15) 
-- Syntax Select * from [dbo].[udf-Create-Range-Date]('2016-10-01','2016-10-02','SS',1) 
+0

你已經達到了我想要的,其實我對編程非常陌生,所以你可以詳細說明如何將這個應用到我的表中而不是udf)的問題,Thanx提前! – 3439027

+1

要清楚。你不想或不能使用UDF? UDF只是生成一個日期範圍。另一種選擇是使用簡單的日期表。無論哪種方式,只是讓我知道,我可以幫助 –

+1

我覺得不得不說這些是現金餘額,而不是每日平均。您一定可以從現貨餘額創建日加權平均餘額,而無需「填補空白」 –

0

查詢作品精細。但它不會做你想要的,因爲你是按日期時間字段進行分組的,2014-01-01 00:00:00與2014-01-01 00:00:01不同。

這是一款基於日

SELECT [AccountCode] ,cast([PostingDate] as date) as date ,sum([Amount]) as 'daily balance' FROM [AccountLedger] WHERE [PostingDate] < '2014-04-01' GROUP BY [AccountCode], cast([PostingDate] as date)

讓我知道,如果這個工程總金額相當簡單的方式。

+0

它也可以在我的查詢中正常工作,問題來自over子句;它工作正常,直到「PARTITION BY Acc」和錯誤出現在「ORDER BY DTE DESC」之外 – 3439027

+0

「PARTITION BY Acc」服務的目的是什麼?我的查詢不會給你你需要的結果嗎? –

0
select 
    AccountCode, 
    convert(date, PostingDate) AS PostingDate, 
    avg(Amount) as Average 
from 
    AccountLedger 
where 
    PostingDate < '2014-04-01' 
group by 
    AccountCode, 
    convert(date, PostingDate) 

http://sqlfiddle.com/#!6/965ec/19