MySQL - 需要检索多个顾问数据 - select SUM 似乎可以防止

MySQL - Need to retrieve muliple advisor's data - select SUM seems to prevent

当前正在使用以下查询:

select distinct(shift_report.advisor), users.team, shift_report.date
from shift_report join users on shift_report.advisor=users.username
where `date` >=20160224
and users.team=1

这为我提供了每个顾问的数据。我想计算每个人花费的总时间,但是,当我将以下内容添加到查询时,它仅为一位顾问的 returns 数据,但时间等似乎是团队总计:

SUM(shift_report.time) as total_time, round(SUM(shift_report.time)/450 * 100,2)
as percentage

当我 运行 以上包括个人的 SUM 时,查询有效。

使用聚合函数时,必须指定 group by 子句才能获得每个分区(在本例中为顾问)的结果

select shift_report.advisor, users.team, shift_report.date,
       SUM(shift_report.time) as total_time, round(SUM(shift_report.time)/450 * 100,2)
from shift_report join users on shift_report.advisor=users.username
where `date` >=20160224
and users.team=1
GROUP BY shift_report.advisor,users.team,shift_report.date

我假设您想按所有这些列(顾问、团队、日期)进行分组,否则,请将它们从子句分组中删除