Quantcast
Channel: Teradata Forums - Database
Viewing all articles
Browse latest Browse all 14773

How to calculate moving sums within variable size windows? - forum topic by bladerunning_Finn

$
0
0

Hi!
I am trying to calculate moving sums and row counts in a query, to do further arithmetic on the said values. For a range of 3 days, for example, I would sum the values given in the rows for those 3 days, and then divide them by the amount of rows in that same time period.
In Oracle, the syntax would be:
      sum(score) over(order by responsedate range between interval '2' day preceding and current row)/
      sum(n) over(order by responsedate range between interval '2' day preceding and current row) as rolling_score
to clarify with an example, if data was:
 
responsedt    score
2013-09-04    1
2013-09-04    1
2013-09-04    1
2013-09-04    -1
2013-09-03    0
2013-09-03    1
2013-09-02    -1
2013-09-02    0
2013-09-02    1
 
...the sum(score) and sum(n) would result in a dataset of:
responsedt    score    sum(n)    sum(score)
2013-09-04    1        
2013-09-04    1        
2013-09-04    1        
2013-09-04    -1        4        2
2013-09-03    0        
2013-09-03    1        2        1
2013-09-02    -1        
2013-09-02    0        
2013-09-02    1        3        0
...with the result being 3/9 = 0.3333...
Trying the Oracle syntax, which looks pretty ANSI to me, I get "expected the word RESET or ')' after ORDER BY clause."
I have experimented now with range_N, where the intervals were not accepted, partitioning by response date, which compiles but does not give me what I want...
Please help. Googling did not help, also, related questions in TD forums have not resulted in an answer I would have seen as answering the question.
Thx,
 

Forums: 

Viewing all articles
Browse latest Browse all 14773

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>