Skip to content

Commit 6fede83

Browse files
author
Indrajeet Mishra
committed
Added Odd and Even Measurements
1 parent 0f4c5c1 commit 6fede83

File tree

4 files changed

+119
-1
lines changed

4 files changed

+119
-1
lines changed

Odd and Even Measurements/README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# [Odd and Even Measurements](https://datalemur.com/questions/odd-even-measurements)
2+
![Star Badge](https://img.shields.io/static/v1?label=%F0%9F%8C%9F&message=If%20Useful&style=style=flat&color=BC4E99)
3+
[![View Main Folder](https://img.shields.io/badge/View-Main_Folder-971901?)](https://github.com/thecoddiwompler/SQL-Practice-Questions/tree/main)
4+
[![View Repositories](https://img.shields.io/badge/View-My_Repositories-blue?logo=GitHub)](https://github.com/thecoddiwompler?tab=repositories)
5+
[![View My Profile](https://img.shields.io/badge/View-My_Profile-green?logo=GitHub)](https://github.com/thecoddiwompler)
6+
7+
---
8+
9+
## This problem was asked in Google Interview.
10+
11+
## 🛠️ Problem Statement
12+
13+
<b>Table Name : Measurements</b>
14+
15+
| Column Name |Type |
16+
| ------------- | ------------- |
17+
| measurement_id | INTEGER |
18+
| measurement_value | DECIMAL |
19+
| measurement_time | TIMESTAMP |
20+
21+
<b>measurement_id is the primary key of this table.</b>
22+
</br>
23+
24+
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.
25+
26+
</br>
27+
28+
Write a SQL 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.
29+
30+
</br>
31+
32+
Definition:
33+
<br>
34+
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.
35+
36+
Return the result table in the order of measurement day.
37+
38+
The query result format is in the following example.
39+
40+
<details>
41+
<summary>
42+
Input
43+
</summary>
44+
</br>
45+
46+
<b> Table Name: Measurements </b></br>
47+
48+
| measurement_id | measurement_value | measurement_time |
49+
|-----------------|-------------------|---------------------------|
50+
| 131233 | 1109.51 | 2022-07-10 09:00:00 |
51+
| 135211 | 1662.74 | 2022-07-10 11:00:00 |
52+
| 523542 | 1246.24 | 2022-07-10 13:15:00 |
53+
| 143562 | 1124.50 | 2022-07-11 15:00:00 |
54+
| 346462 | 1234.14 | 2022-07-11 16:45:00 |
55+
56+
57+
</details>
58+
59+
<details>
60+
<summary>
61+
Output
62+
</summary>
63+
</br>
64+
65+
| measurement_day | odd_sum | even_sum |
66+
|-------------------------|----------|----------|
67+
| 2022-07-10 00:00:00 | 2355.75 | 1662.74 |
68+
| 2022-07-11 00:00:00 | 1124.50 | 1234.14 |
69+
70+
71+
</details>
72+
73+
---

Odd and Even Measurements/schema.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
CREATE TABLE measurements (
2+
measurement_id INTEGER,
3+
measurement_value DECIMAL,
4+
measurement_time TIMESTAMP
5+
);
6+
7+
INSERT INTO measurements (measurement_id, measurement_value, measurement_time)
8+
VALUES
9+
(131233, 1109.51, '2022-07-10 09:00:00'),
10+
(135211, 1662.74, '2022-07-10 11:00:00'),
11+
(523542, 1246.24, '2022-07-10 13:15:00'),
12+
(143562, 1124.50, '2022-07-11 15:00:00'),
13+
(346462, 1234.14, '2022-07-11 16:45:00');
14+
15+
COMMIT;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
WITH cte AS (
2+
SELECT
3+
measurement_id,
4+
measurement_value,
5+
measurement_time,
6+
ROW_NUMBER() OVER(
7+
PARTITION BY TO_CHAR(measurement_time, 'YYYY-MM-DD')
8+
ORDER BY
9+
measurement_time
10+
) rn
11+
FROM
12+
measurements
13+
)
14+
SELECT
15+
DATE_TRUNC('day', measurement_time) measurement_day,
16+
SUM(measurement_value) FILTER(
17+
WHERE
18+
MOD(rn, 2) = 1
19+
) odd_sum,
20+
SUM(measurement_value) FILTER(
21+
WHERE
22+
MOD(rn, 2) = 0
23+
) even_sum
24+
FROM
25+
cte
26+
GROUP BY
27+
DATE_TRUNC('day', measurement_time)
28+
ORDER BY
29+
DATE_TRUNC('day', measurement_time);

iqsql.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
2. Employees Hiring [Difficult]
33
3. Highest-Grossing Items
44
4. Manager with at least 5 direct reportees
5-
5. Qualifying Criteria
5+
5. Odd and Even Measurements
6+
6. Qualifying Criteria

0 commit comments

Comments
 (0)