File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change @@ -105,3 +105,43 @@ from visitor_data;
105
105
| 5 | 2022-05-01 | 2 |
106
106
| 5 | 2022-06-01 | 2 |
107
107
| 5 | 2022-08-01 | 2 |
108
+
109
+
110
+ ## Example 3. Webpage Views. Running sum of pageviews by customer in the last 60 days
111
+
112
+ ### Input data
113
+
114
+ | DATE_VIEWED| VIEWS| CUSTOMER_ID|
115
+ | ------------| ----| ---|
116
+ | 2020-01-01 | 1 | a |
117
+ | 2020-01-15 | 2 | b |
118
+ | 2020-01-20 | 1 | a |
119
+ | 2020-01-25 | 20 | b |
120
+ | 2020-02-15 | 1 | a |
121
+ | 2020-03-15 | 2 | b |
122
+ | 2020-04-15 | 1 | a |
123
+ | 2020-05-15 | 2 | b |
124
+
125
+ ### SQL Query
126
+
127
+ ``` sql
128
+ select
129
+ *
130
+ , sum (views) over (partition by customer_id
131
+ order by date_viewed
132
+ range between interval ' 59 day' preceding and current row)
133
+ from page_views
134
+ ;
135
+ ```
136
+ ### Query Output
137
+
138
+ | DATE_VIEWED| VIEWS| CUSTOMER_ID| ROLLING_SUM_OF_VIEWS_LAST_60_DAYS|
139
+ | ------------| ----| ---| ----|
140
+ | 2020-01-01 | 1 | a | 1 |
141
+ | 2020-01-20 | 1 | a | 2 |
142
+ | 2020-02-15 | 1 | a | 3 |
143
+ | 2020-04-15 | 1 | a | 1 |
144
+ | 2020-01-15 | 2 | b | 2 |
145
+ | 2020-01-25 | 20 | b | 22 |
146
+ | 2020-03-15 | 2 | b | 22 |
147
+ | 2020-05-15 | 2 | b | 2 |
You can’t perform that action at this time.
0 commit comments