我是新來的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