Skip to content

Commit 1c57927

Browse files
committed
Windows Function - Lead() and Lag()
1 parent 9d3a703 commit 1c57927

File tree

2 files changed

+45
-0
lines changed
  • Advanced SQL for Data Science - Time Series/02.Commonly used Functions for Time Series

2 files changed

+45
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/******** Commonly Used Functions for Time Series ***********/
2+
3+
/* we will assume last digit of server_id is department id */
4+
CREATE VIEW time_series.vw_utilization AS(
5+
SELECT *, server_id % 10 AS dept_id
6+
FROM time_series.utilization
7+
);
8+
9+
SELECT * FROM time_series.vw_utilization
10+
LIMIT 5;
11+
12+
13+
14+
--------------------- LEAD() function ---------------------------
15+
-- LEAD() looks forwards and allows us to compare condition with the next nth row of current row.
16+
-- we can also put offset of how many next rows we want to get.
17+
18+
-- next 1 row
19+
SELECT dept_id, server_id, cpu_utilization,
20+
LEAD(cpu_utilization) OVER (PARTITION BY dept_id ORDER BY cpu_utilization DESC)
21+
FROM time_series.vw_utilization
22+
WHERE event_time BETWEEN '2019-03-05' AND '2019-03-06';
23+
24+
-- next 3 row
25+
SELECT dept_id, server_id, cpu_utilization,
26+
LEAD(cpu_utilization, 3) OVER (PARTITION BY dept_id ORDER BY cpu_utilization DESC)
27+
FROM time_series.vw_utilization
28+
WHERE event_time BETWEEN '2019-03-05' AND '2019-03-06';
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/******** Commonly Used Functions for Time Series ***********/
2+
3+
--------------------- LAG() function ---------------------------
4+
-- to reference rows relative to the currently processed rows.
5+
-- LAG() looks backwards and allows us to compare condition with the previous nth row of current row.
6+
7+
SELECT dept_id, server_id, cpu_utilization,
8+
LAG(cpu_utilization) OVER (PARTITION BY dept_id ORDER BY cpu_utilization DESC)
9+
FROM time_series.vw_utilization
10+
WHERE event_time BETWEEN '2019-03-05' AND '2019-03-06';
11+
12+
13+
-- with offset of 10, looking backwards to previous 10th row from the current one
14+
SELECT dept_id, server_id, cpu_utilization,
15+
LAG(cpu_utilization, 10) OVER (PARTITION BY dept_id ORDER BY cpu_utilization DESC)
16+
FROM time_series.vw_utilization
17+
WHERE event_time BETWEEN '2019-03-05' AND '2019-03-06';

0 commit comments

Comments
 (0)