我正在从数据库进行一些查询来制作图表,可能需要的图表是每月一组值的中位数。我知道如何获得整个数据集的中位数,但出于某种原因,我不能让GROUP BY MONTH(..)将月份分开,以便返回每月的中位数。
以下是我拥有的数据,我希望每个月能够获得每个月的中值,无论构建类型如何。
'Development', 1013.0164, 'June'
'Development', 1170.8999, 'July'
'Development', 671.2837, 'August'
'Flash Assets', 2961.3832, 'June'
'Flash Assets', 6662.2335, 'July'
'Flash Assets', 3902.5000, 'August'
'Release', 54.5499, 'June'
'Release', 62.4832, 'July'
'Release', 398.8500, 'August'
'Repackage', 1360.0834, 'June'
'Repackage', 6286.8505, 'July'
'Repackage', 1274.7833, 'August'
'Component', 16378.0161, 'June'
'Component', 6063.5482, 'July'
'Component', 23663.2496, 'August'
'Source Diff', 1503.8834, 'June'
'Source Diff', 1051.4500, 'July'
'Source Diff', 73.7002, 'August'
我想结束这个,
June, XXXX
July, XXXX
August, XXXX
感谢。
编辑:当前查询
这是我现在用来获取整体中位数的查询,我不知道如何将其翻译为每月获得它。
SELECT t.Data AS 'Median' FROM
(SELECT CEIL(COUNT(*)/2.0) as 'Middle', s.Data as 'Data' FROM
(SELECT bt.name as 'Labels',
SUM(TIME_TO_SEC(TIMEDIFF(bs.eventtime, b.submittime))/60.0)
AS 'Data', MONTHNAME(b.submittime) FROM builds b
JOIN buildstatuses bs ON bs.buildid = b.id
JOIN buildtypes bt ON bt.id = b.buildtype
WHERE MONTH(b.submittime) BETWEEN MONTH(CURDATE())-2 AND MONTH(CURDATE())
AND bs.status LIKE 'Started HANDLER'
GROUP BY b.buildtype, MONTH(b.submittime) ORDER BY 'Data' ) s )t;
答案 0 :(得分:1)
人at the mysql's aggregate functions discussion page以临时表结束。在那里搜索“中位数”。
答案 1 :(得分:1)
如果你的价值总是在浮动......你可以试试这个 -
假设Data
是值列
select mth,
substring_index
(
substring_index
(
group_concat(Data order by Data), ',',
count(*)/2+1
),
',', -1
)
from your_tables
group by mth;
group_concat
将按升序对月份值进行排序substring_index
将从第一个值返回到中间值+1更多位置substring_index
将帮助您确定最合适的值(即中位数)