Overview
The meteorological system aggregates hourly weather observations into daily summaries for trend analysis and reporting. This involves temperature unit conversion, statistical aggregations, and calculating temperature changes over time using SQL window functions.Daily Aggregation Table
Table Structure
Theclima_diario_f table stores daily aggregated weather data with temperatures in Fahrenheit.
Design Differences from Source Table
| Aspect | clima (hourly) | clima_diario_f (daily) |
|---|---|---|
| Granularity | fechaHora DATETIME | fecha DATE |
| Temperature | tempCelsius DECIMAL(5,2) | temperatura_f DECIMAL(6,2) |
| Temperature Unit | Celsius | Fahrenheit |
| Cloud Coverage | coberturaNubes | cobertura_nubes |
| Unique Constraint | (localidad, fechaHora) | (localidad, fecha) |
| Purpose | Raw observations | Daily aggregates |
Aggregation Logic
Complete Aggregation Query
Aggregation Components
1. Temperature Conversion and Averaging
- Calculate average Celsius temperature for the day:
AVG(tempCelsius) - Convert to Fahrenheit:
(celsius * 9/5) + 32 - Round to 2 decimal places:
ROUND(..., 2)
- Hourly temps for Bogota on 2025-01-01: 15°C, 18°C, 22°C, 20°C
- Average: (15 + 18 + 22 + 20) / 4 = 18.75°C
- Fahrenheit: (18.75 × 9/5) + 32 = 65.75°F
- Multiply by 9/5 (or 1.8) to convert Celsius scale to Fahrenheit scale
- Add 32 to adjust for the offset between scales (0°C = 32°F)
2. Date Grouping
- Extract date portion from timestamp:
DATE(fechaHora) - Group all hourly observations from the same location and day
- Example: ‘2025-01-01 08:00:00’, ‘2025-01-01 14:00:00’, ‘2025-01-01 20:00:00’ → ‘2025-01-01’
3. Cloud Coverage (Maximum)
- ‘Minima’ < ‘Parcial’ < ‘Total’
- Hourly observations: ‘Minima’, ‘Parcial’, ‘Parcial’, ‘Total’
- Daily summary: ‘Total’
4. Environmental Metrics (Averages)
- UV Index: Average exposure throughout the day
- Atmospheric Pressure: Mean pressure trends
- Wind Speed: Average wind conditions
5. Location Context
localidad and pais.
Temperature Delta Calculations
Purpose
Calculate temperature change from the previous observation for the same location, enabling trend analysis and detecting rapid weather changes.Adding Delta Columns
Hourly Table (Celsius)
Daily Table (Fahrenheit)
DECIMAL(6,2) allows for:
- Negative deltas (temperature drops): -9999.99
- Positive deltas (temperature rises): +9999.99
- Precision to 0.01 degrees
Window Function Approach
Hourly Delta Calculation
Daily Delta Calculation
Understanding LAG Window Function
Syntax
Components
LAG Function
Purpose: Access data from a previous row in the result set without using a self-join. Returns: The value ofcolumn_name from the previous row, or NULL if no previous row exists.
PARTITION BY localidad
Purpose: Create separate “windows” for each location. Effect: LAG only looks at previous rows within the same location, not across different locations. Example:ORDER BY fechaHora (or fecha)
Purpose: Define which row is “previous” based on chronological order. Effect: LAG retrieves the temperature from the immediately preceding timestamp for that location.Delta Calculation Walkthrough
Example Data: Bogota Hourly Temperatures
| id | localidad | fechaHora | tempCelsius | LAG(tempCelsius) | delta_temp_c |
|---|---|---|---|---|---|
| 1 | Bogota | 2025-01-01 08:00 | 19.00 | NULL | NULL |
| 2 | Bogota | 2025-01-20 08:00 | 20.00 | 19.00 | +1.00 |
| 3 | Bogota | 2025-02-15 09:00 | 22.00 | 20.00 | +2.00 |
| 4 | Bogota | 2025-03-10 10:00 | 25.00 | 22.00 | +3.00 |
| 5 | Bogota | 2026-01-01 07:00 | 18.00 | 25.00 | -7.00 |
Calculation Details
Row 1 (id=1):- Current: 19°C
- Previous: NULL (first observation for Bogota)
- Delta: NULL
- Current: 20°C
- Previous: 19°C (from id=1)
- Delta: 20 - 19 = +1.00°C
- Interpretation: Temperature increased by 1 degree
- Current: 18°C
- Previous: 25°C (from id=4)
- Delta: 18 - 25 = -7.00°C
- Interpretation: Significant temperature drop of 7 degrees
Query Pattern Breakdown
Step 1: Subquery with Window Function
id and calculated delta for each row.
Step 2: JOIN to Original Table
id.
Step 3: UPDATE
delta_temp_c column.
Use Cases for Delta Analysis
1. Rapid Weather Change Detection
2. Warming/Cooling Trends
3. Consecutive Increase/Decrease Patterns
4. Daily Volatility Analysis
Advantages of Window Functions
vs. Self-Joins
Traditional Self-Join Approach:Benefits
- Performance: Single pass through data vs. multiple table scans
- Readability: Intent is clear and concise
- Correctness: Handles edge cases (first row) automatically
- Flexibility: Easy to access rows at different offsets (LAG(col, 2) for 2 rows back)
Verification Queries
Check Daily Aggregation Results
Verify Temperature Deltas
Validate Conversion Formula
Best Practices
1. Aggregation Scheduling
For production systems, run aggregation as a scheduled job:2. Incremental Updates
Avoid re-aggregating all historical data:3. Delta Calculation Timing
Recalculate deltas after:- New data insertion
- Data corrections
- Historical data backfills