Select语句中的Count的平均值

时间:2016-05-04 05:23:16

标签: php mysql

我似乎无法解决这个谜语。我有以下声明:

SELECT schedule_test.ID, LEFT(schedule_test.Course,2) AS 'Type', COUNT(*) AS  
'Count'
FROM schedule_test
GROUP BY ID, Type

它回应了以下内容:

enter image description here

我可以在Select语句中添加什么来添加第4列,其中包含每种类型的平均值?换句话说,所有员工中“HS”类型的平均数是多少?尝试添加AVG(COUNT(*)),但收到错误Invalid use of group function

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

SELECT .......,
AVG(Count) AS average,
AVG(Count WHEN Type = 'HS' THEN Count ELSE NULL END) AS conditionalAverage
FROM schedule_test
GROUP BY ID, Type;