テーブルから開始。計算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または私の場合は)簡単な日付テーブルで
ACC、DTE *でオーダーとは何ですか? dte *とは何ですか?この場合、私はエラーメッセージがボリュームを話すと信じています...そしてそのかなり正確です。 – JonH
この投稿は研究努力を示していません。 – dfundako
DTEは、 の投稿日時をテキストエディタで書いています。 問題はover節から出てきます。 「PARTITION BY Acc」と「ORDER BY DTE DESC」の間にエラーが発生します。 – 3439027