我通过选择过去六周内完成评估的所有员工数据来编辑查询。从逻辑上讲,如果每个员工在两周内完成评估,那么每个员工应该两次,但此查询显示单个记录。
select
AssessmentEmployee.
EmployeeName,
AVG(AssessmentListing.AssessmentScore),
DATE_FORMAT((STR_TO_DATE(`AssessmentSubmittedDatetime`, '%d-%b-%Y %I:%i %p')) , '%Y-%m-%v') as _month
from AssessmentEmployee
LEFT JOIN AssessmentListing
ON AssessmentEmployee.AssessmentID=AssessmentListing.AssessmentID
WHERE (STR_TO_DATE(`AssessmentSubmittedDatetime`, '%d-%b-%Y %I:%i %p') >= DATE_FORMAT(NOW() - INTERVAL 6 Week, '%Y' ))
group by AssessmentEmployee.EmployeeName
我有以下表格,我正在使用。
AssessmentEmployee
ID
AssessmentID
EmployeeName
其他表格是评估列表
ID
AssessmentID
AssessmentSubmittedDateTime
AssessmentScore
我想让那些在过去的性生活周中得分/完成评估的员工及其平均分数。
评估列表数据列的示例
ID AssessmentID AssessmentSubmittedDatetime AssessmentScore
1 040416024720 04-Apr-2016 02:48 PM 50
答案 0 :(得分:0)
你正在做一个平均值,所以如果他们两次得分,它将平均得分为2分。
答案 1 :(得分:0)
请尝试以下查询:
select
AssessmentEmployee.
EmployeeName,
AVG(AssessmentListing.AssessmentScore),
DATE_FORMAT((STR_TO_DATE(`AssessmentSubmittedDatetime`, '%d-%b-%Y %I:%i %p')) , '%Y-%v') as year_week
from AssessmentEmployee
LEFT JOIN AssessmentListing
ON AssessmentEmployee.AssessmentID=AssessmentListing.AssessmentID
WHERE UNIX_TIMESTAMP(DATE_FORMAT(STR_TO_DATE(`AssessmentSubmittedDatetime`,'%d-%b-%Y %I:%i %p'),'%Y-%m-%d')) >= UNIX_TIMESTAMP(CURDATE() - INTERVAL 6 WEEK)
group by AssessmentEmployee.EmployeeName, year_week;
您不应将日期/时间存储为字符串。否则,在处理这些繁琐的工作时,请接受它们。
答案 2 :(得分:0)
SELECT *, SUM(AssessmentScore) as total, SUM(AssessmentScore)/6 as avg
FROM `assessmentlisting`
WHERE STR_TO_DATE(`AssessmentSubmittedDatetime`, '%d-%b-%Y %I:%i %p') > DATE_FORMAT(NOW() - INTERVAL 6 Week, '%Y-%m-%d %I:%i %p' )
GROUP BY assessmentlisting.AssessmentID
希望它能奏效。