-
Notifications
You must be signed in to change notification settings - Fork 518
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
...SQL for Data Science - Time Series/02.Commonly used Functions for Time Series/01.Lead.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/******** Commonly Used Functions for Time Series ***********/ | ||
|
||
/* we will assume last digit of server_id is department id */ | ||
CREATE VIEW time_series.vw_utilization AS( | ||
SELECT *, server_id % 10 AS dept_id | ||
FROM time_series.utilization | ||
); | ||
|
||
SELECT * FROM time_series.vw_utilization | ||
LIMIT 5; | ||
|
||
|
||
|
||
--------------------- LEAD() function --------------------------- | ||
-- LEAD() looks forwards and allows us to compare condition with the next nth row of current row. | ||
-- we can also put offset of how many next rows we want to get. | ||
|
||
-- next 1 row | ||
SELECT dept_id, server_id, cpu_utilization, | ||
LEAD(cpu_utilization) OVER (PARTITION BY dept_id ORDER BY cpu_utilization DESC) | ||
FROM time_series.vw_utilization | ||
WHERE event_time BETWEEN '2019-03-05' AND '2019-03-06'; | ||
|
||
-- next 3 row | ||
SELECT dept_id, server_id, cpu_utilization, | ||
LEAD(cpu_utilization, 3) OVER (PARTITION BY dept_id ORDER BY cpu_utilization DESC) | ||
FROM time_series.vw_utilization | ||
WHERE event_time BETWEEN '2019-03-05' AND '2019-03-06'; |
17 changes: 17 additions & 0 deletions
17
... SQL for Data Science - Time Series/02.Commonly used Functions for Time Series/02.Lag.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/******** Commonly Used Functions for Time Series ***********/ | ||
|
||
--------------------- LAG() function --------------------------- | ||
-- to reference rows relative to the currently processed rows. | ||
-- LAG() looks backwards and allows us to compare condition with the previous nth row of current row. | ||
|
||
SELECT dept_id, server_id, cpu_utilization, | ||
LAG(cpu_utilization) OVER (PARTITION BY dept_id ORDER BY cpu_utilization DESC) | ||
FROM time_series.vw_utilization | ||
WHERE event_time BETWEEN '2019-03-05' AND '2019-03-06'; | ||
|
||
|
||
-- with offset of 10, looking backwards to previous 10th row from the current one | ||
SELECT dept_id, server_id, cpu_utilization, | ||
LAG(cpu_utilization, 10) OVER (PARTITION BY dept_id ORDER BY cpu_utilization DESC) | ||
FROM time_series.vw_utilization | ||
WHERE event_time BETWEEN '2019-03-05' AND '2019-03-06'; |