File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/************ Common Table Expression and Recursion *************/
2+
3+
/* CTE is basically a way of creating Temporary Table */
4+
5+
/* daily average temp */
6+
WITH daily_average_temp AS(
7+
SELECT DATE_TRUNC('day', event_time) AS event_date,
8+
AVG(temp_celcius) AS avg_temp
9+
FROM time_series.location_temp
10+
GROUP BY DATE_TRUNC('day', event_time)
11+
)
12+
SELECT event_date, avg_temp
13+
FROM daily_average_temp;
14+
15+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/****** Calculating Aggregates over windows *******/
2+
3+
SELECT server_id,cpu_utilization,
4+
AVG(cpu_utilization) OVER (PARTITION BY server_id)
5+
FROM time_series.utilization
6+
WHERE event_time BETWEEN '2019-03-05' AND '2019-03-06';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/***** Comparison (comparing to Previous Period) ********/
2+
3+
-- will compare the avg temp of current day with avg temp of previous day
4+
-- based on this, we can do comparison of month to month, year to year, etc.
5+
WITH avg_daily_temp AS(
6+
SELECT
7+
DATE_TRUNC('day', event_time) AS event_day,
8+
AVG(temp_celcius) AS avg_temp
9+
FROM time_series.location_temp
10+
GROUP BY DATE_TRUNC('day', event_time)
11+
)
12+
SELECT
13+
d1.event_day, d1.avg_temp,
14+
(SELECT d2.avg_temp
15+
FROM avg_daily_temp d2
16+
WHERE d2.event_day = d1.event_day - INTERVAL '1' day)
17+
FROM avg_daily_temp d1;

0 commit comments

Comments
 (0)