Odd/Even Measurements

ASKED IN INTERVIEW
4pts
Tesla

Problem Statement

Tesla sensors take multiple measurements throughout the day. To analyze the data, you need to sum the measurements based on their sequence order within each day.

Write an SQL query to calculate the sum of measurements for odd-numbered observations and even-numbered observations for each day.

Rules:

  • Within each day, measurements are ordered by measurement_time ascending.
  • The first measurement of the day is sequence 1 (Odd), the second is 2 (Even), etc.
  • Output columns: measurement_day, odd_sum, and even_sum.
  • Results must be sorted by measurement_day in ascending order.
Tests your understanding of
Window Functions, Conditional Aggregation and Sequencing

Input Tables

measurements
measurement_id(INTEGER)measurement_value(DECIMAL)measurement_time(TIMESTAMP)
110.52023-01-01 09:00:00
2202023-01-01 11:00:00
35.52023-01-01 13:00:00
4152023-01-02 08:30:00
5302023-01-02 10:00:00
622023-01-03 09:00:00
782023-01-03 10:00:00
812.52023-01-03 12:00:00
91.52023-01-03 14:00:00
sensors
sensor_id(INTEGER)sensor_type(VARCHAR)
1Pressure
2Temperature
sensor_mapping
measurement_id(INTEGER)sensor_id(INTEGER)
11
22

Expected Output

measurement_day(DATE)odd_sum(DECIMAL)even_sum(DECIMAL)
2023-01-011620
2023-01-021530
2023-01-0314.59.5

Tags

MediumASKED IN INTERVIEWWindow FunctionsConditional AggregationSequencing
15-20 min
50%

Hints