2016-05-23 21 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'.* 

どのようにして必要な結果を得ることができますか? (日付範囲を生成するために、UDFまたは私の場合は)簡単な日付テーブルで

+0

ACC、DTE *でオーダーとは何ですか? dte *とは何ですか?この場合、私はエラーメッセージがボリュームを話すと信じています...そしてそのかなり正確です。 – JonH

+0

この投稿は研究努力を示していません。 – dfundako

+0

DTEは、 の投稿日時をテキストエディタで書いています。 問題はover節から出てきます。 「PARTITION BY Acc」と「ORDER BY DTE DESC」の間にエラーが発生します。 – 3439027

答えて

1

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

それは私の質問でもうまくいきます。問題は句を超えています。 「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