我是新来的SQL和一直在努力对南位数,例如,我有三个(万元)行,每一张桌子十个数字(或空):如何计算Mysql中一行的中位数?
row1: 1,2,3,null,4,5,6,7,8,9
----------
row2: 2,4,null,6,8,2,1,0,9,10
----------
row3: 1,1,1,1,null,7,2,9,9,9
----------
如何获得nan-median of each row?
我是新来的SQL和一直在努力对南位数,例如,我有三个(万元)行,每一张桌子十个数字(或空):如何计算Mysql中一行的中位数?
row1: 1,2,3,null,4,5,6,7,8,9
----------
row2: 2,4,null,6,8,2,1,0,9,10
----------
row3: 1,1,1,1,null,7,2,9,9,9
----------
如何获得nan-median of each row?
根据你的问题,如果你有10列,你想计算。 诀窍是用Coalesce
函数来避免空值。 使用此逻辑:通过排序在ASC行应是在壳体中值如果列计数为偶数第一50%的行
Select (coalesce(Col1, 0) + coalesce(Col2, 0) + coalesce(Col3, 0)+ coalesce(Col4, 0)+coalesce(Col5, 0)+coalesce(Col6, 0)+coalesce(Col7, 0)+coalesce(Col8, 0)+coalesce(Col9, 0)+.coalesce(Col10, 0))/10
from yourtable
这就是平均值。用于分隔元素上半部分和下半部分的值的中间值(例如:9个元素列表的第5个元素以及10个元素列表的第5个元素和第6个元素的平均值)。 – Solarflare
select(
(select MAX(cols) from
(select TOP 50 PERCENT cols from table ORDER BY cols))
+
(select MIN(cols) from
(select TOP 50 PERCENT cols from table ORDER BY cols DESC))
)/2
Median
选择最大值。通过对desc中的行进行排序来选择下一个50%行中的最小值,如果列计数为偶数,则这将是中值或否则(firsthalf + min of secondhalf)/ 2的最大值将是中值。
这可能有所帮助。 https://stackoverflow.com/questions/1342898/function-to-calculate-median-in-sql-server –
如果我理解正确,该职位是关于计算列的中位数,我想弄清楚如何计算一排的中位数。 – noringname
你的行是否有主键? – MBurnham