You are given a table Numbers with the following structure:
num: The actual number (primary key)frequency: How many times this number appears
The median is the value separating the higher half from the lower half of a data sample. When we "decompress" the table by repeating each number according to its frequency, we need to find the median of all these numbers.
Write a SQL solution to find the median of all numbers after decompressing the table. Round the result to one decimal place.
Table Schema
| Column Name | Type | Description |
|---|---|---|
num
PK
|
int | The actual number value (primary key) |
frequency
|
int | How many times this number appears |
Input & Output
| num | frequency |
|---|---|
| 0 | 7 |
| 1 | 1 |
| 2 | 3 |
| median |
|---|
| 0 |
After decompressing: [0,0,0,0,0,0,0,1,2,2,2] (11 numbers total). The median is the 6th number when sorted, which is 0. Result: 0.0
| num | frequency |
|---|---|
| 1 | 2 |
| 2 | 2 |
| median |
|---|
| 1.5 |
After decompressing: [1,1,2,2] (4 numbers total). The median is the average of 2nd and 3rd numbers: (1+2)/2 = 1.5
| num | frequency |
|---|---|
| 5 | 10 |
| median |
|---|
| 5 |
All 10 numbers are 5. The median of [5,5,5,5,5,5,5,5,5,5] is clearly 5.0
Constraints
-
1 ≤ Numbers.num ≤ 500 -
1 ≤ Numbers.frequency ≤ 10^4 -
The total frequency is guaranteed to be at least 1 -
Round the median to one decimal place