File tree Expand file tree Collapse file tree 2 files changed +45
-0
lines changed
Advanced SQL for Data Science - Time Series/02.Commonly used Functions for Time Series Expand file tree Collapse file tree 2 files changed +45
-0
lines changed Original file line number Diff line number Diff line change 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' ;
Original file line number Diff line number Diff line change 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' ;
You can’t perform that action at this time.
0 commit comments