I want to compute the Median of y
in sub groups of this simple xy_table
:
x | y --groups--> gid | x | y --medians--> gid | x | y
------- ------------- -------------
0.1 | 4 0.0 | 0.1 | 4 0.0 | 0.1 | 4
0.2 | 3 0.0 | 0.2 | 3 | |
0.7 | 5 1.0 | 0.7 | 5 1.0 | 0.7 | 5
1.5 | 1 2.0 | 1.5 | 1 | |
1.9 | 6 2.0 | 1.9 | 6 | |
2.1 | 5 2.0 | 2.1 | 5 2.0 | 2.1 | 5
2.7 | 1 3.0 | 2.7 | 1 3.0 | 2.7 | 1
In this example every x
is unique and the table is already sorted by x
.
I now want to GROUP BY round(x)
and get the tuple that holds the median of y
in each group.
I can already compute the median for the whole table with this ranking query:
SELECT a.x, a.y FROM xy_table a,xy_table b
WHERE a.y >= b.y
GROUP BY a.x, a.y
HAVING count(*) = (SELECT round((count(*)+1)/2) FROM xy_table)
Output: 0.1, 4.0
But I did not yet succeed writing a query to compute the median for sub groups.
Attention: I do not have a median()
aggregation function available. Please also do not propose solutions with special PARTITION
, RANK
, or QUANTILE
statements (as found in similar but too vendor specific SO questions). I need plain SQL (i.e., compatible to SQLite without median()
function)
Edit: I was actually looking for the Medoid and not the Median.
gid
changed to 0.1 and neitherx
nory
values are medians for the 0.0 group – Charterhousegid
(group ID) groups. And I assume the median of [4,3] is 4 (the bigger value) in such cases. – Carreno