Skip to content

Commit 4502af9

Browse files
Update RANGE-clause-in-window-functions-practical-examples.md
1 parent 10eccd1 commit 4502af9

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

RANGE-clause-in-window-functions-practical-examples.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,43 @@ from visitor_data;
105105
| 5 | 2022-05-01 | 2 |
106106
| 5 | 2022-06-01 | 2 |
107107
| 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 |

0 commit comments

Comments
 (0)