2016-06-20 7 views
1

私は、トランザクションテーブル内の顧客あたりのローリング24時間支出額を計算するために分析関数を使用しています。関数は以前はtimestamp(9)に変更されていましたが、trx_datetimeフィールドは最近動作しました。Oracle 11g Analytics関数SUM

select sum(th.amount) 
     over(partition by th.customer_id 
     order by th.trx_datetime 
     range between 1 preceding and 0 following) as rolling_trx_amt 
from transactions th; 

今質問を実行すると、次のエラーが表示されます。

ORA-00902: invalid datatype 
00902. 00000 - "invalid datatype" 

私は解決策を見つけるために時間を検索しth.trx_datetimeに無数の変換を試みたが、エラーを修正する方法を見つけることができませんでしたしました。アナリティクス機能の注文をタイムスタンプで処理する方法を知っている場合は、お知らせください。

答えて

0

範囲が整数を使用しているため、タイムスタンプ算術で間隔が使用されているのに対し、範囲では整数が使用されているため、エラーが発生しています。

そのため、あなたはそうのように、numtodsintervalを使用していますが、これは間隔にあなたの範囲を変換する必要があります。

select sum(th.amount) 
     over(partition by th.customer_id 
     order by th.trx_datetime 
     range between numtodsinterval(1, 'DAY') preceding 
        and numtodsinterval(0, 'DAY') following) as rolling_trx_amt 
from transactions th; 

あなたはまた、としてこれを書き換えることができます:あなたが使用している場合ので

select sum(th.amount) 
     over(partition by th.customer_id 
     order by th.trx_datetime 
     range between numtodsinterval(1, 'DAY') preceding 
        and current row) as rolling_trx_amt 
from transactions th; 

windowing句を指定すると、 "現在の行"は "現在の行と同じ値の行"になります。

+1

ありがとうございました。それはトリックでした! –