I have a dataset with columns - customer, product, and quantity. I want to find the median of quantity with respect to different products. Assuming we need to work only for an odd number of rows.
Functions like With, Join, aggregate functions like count, avg, max, min, etc are allowed. A solution using nested subqueries would be ideal for this question.
So far I have listed down the quantity and product in sorted order and found the median number using ROUND((COUNT(QUANT) / 2) + 1) and now I need to find that median row without using any window function.
Input
Product | Quantity |
---|---|
A | 1 |
A | 2 |
A | 3 |
B | 5 |
B | 6 |
B | 7 |
C | 11 |
C | 13 |
C | 15 |
D | 4 |
D | 5 |
D | 6 |
Output
Product | Median |
---|---|
A | 2 |
B | 6 |
C | 13 |
D | 5 |