logo

Back to questions

Odd and Even Measurements [Google SQL Interview Question]

Medium

This is the same question as problem #28 in the SQL Chapter of Ace the Data Science Interview!

Assume you're given a table with measurement values obtained from a Google sensor over multiple days with measurements taken multiple times within each day.

Write a query to calculate the sum of odd-numbered and even-numbered measurements separately for a particular day and display the results in two different columns. Refer to the Example Output below for the desired format.

Definition:

  • Within a day, measurements taken at 1st, 3rd, and 5th times are considered odd-numbered measurements, and measurements taken at 2nd, 4th, and 6th times are considered even-numbered measurements.

Effective April 15th, 2023, the question and solution for this question have been revised.

Table:

Column NameType
measurement_idinteger
measurement_valuedecimal
measurement_timedatetime

Example Input:

measurement_idmeasurement_valuemeasurement_time
1312331109.5107/10/2022 09:00:00
1352111662.7407/10/2022 11:00:00
5235421246.2407/10/2022 13:15:00
1435621124.5007/11/2022 15:00:00
3464621234.1407/11/2022 16:45:00

Example Output:

measurement_dayodd_sumeven_sum
07/10/2022 00:00:002355.751662.74
07/11/2022 00:00:001124.501234.14

Explanation

Based on the results,

  • On 07/10/2022, the sum of the odd-numbered measurements is 2355.75, while the sum of the even-numbered measurements is 1662.74.
  • On 07/11/2022, there are only two measurements available. The sum of the odd-numbered measurements is 1124.50, and the sum of the even-numbered measurements is 1234.14.

The dataset you are querying against may have different input & output - this is just an example!

p.s. read this blog post for more Google SQL Interview Questions

PostgreSQL 14