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

标签 mysql sum distinct

当前正在使用以下查询:

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/

相关文章:

mysql - 使用条件 mysql 获取最大 COUNT(DISTINCT)

Python:加入两个相同大小的列表

MySQL 标记系统 - 每月热门关键词

MySQL - 当日销售的特定产品的平均数量

mysql - 如何加入 2 个查询以在 sql 中按位置对用户进行排序

java - 如何计算最小值、最大值、总和和平均值

Java:二维数组总计未完成

php - 在一种情况下根据特定列的不同返回结果

php - 带有 LIKE 返回错误行的 MySQL

mysql - 将 XML 读入 SQL 表