假设您所有的范围值都是单个范围,而不是逗号分隔的列表,例如,您需要执行一些子字符串操作来检索两侧的值-
并使用算术手动平均它们。 AVG()
是聚合函数,不能用于此目的。
CREATE TABLE HrsPerWk_avg
SELECT
CrsTitle,
/* Extract the min/max values with substring operations, add them, divide by 2 */
/* SUBSTRING_INDEX() gets the first value before - */
(SUBSTRING_INDEX(HoursPerWkincClass, '-', 1) +
/* SUBSTR() and LOCATE() get the second value after - */
/* Both are added and divided by 2 */
SUBSTR(HoursPerWkincClass, LOCATE('-', HoursPerWkincClass)+1)) / 2
FROM FCQ
GROUP BY CrsTitle
这是一个示例:http ://sqlfiddle.com/#!2/96510/2
更新
要支持16+
您在上面发布的值,您需要有条件地匹配类似的值,并使用子字符串操作LOCATE()
修剪掉。+
SELECT
CrsTitle,
CASE
/* Remove + if present */
WHEN LOCATE('+', HoursPerWkincClass) >= 1 THEN REPLACE(HoursPerWkincClass, '+', '')
ELSE
/* Otherwise use the range finding stuff from above... */
(SUBSTRING_INDEX(HoursPerWkincClass, '-', 1) +
SUBSTR(HoursPerWkincClass, LOCATE('-', HoursPerWkincClass)+1)) / 2
END AS the_avg
FROM FCQ
http://sqlfiddle.com/#!2/bdd43/2