当前正在使用以下查询:
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
这为我提供了每个顾问的数据。我想计算每个花费的总时间,但是,当我将以下内容添加到查询时,它只返回一个顾问的数据,但时间等似乎是团队总计:
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
我假设你想按所有这些列(顾问、团队、日期)分组,如果不是,请将它们从分组子句中删除
关于MySQL - 需要检索多个顾问的数据 - 选择 SUM 似乎可以防止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35605393/