SELECT count( DISTINCT homework_id ) finishHomeworkCount, count( 1 ) AS homeworkTitleCount, COALESCE ( ROUND(( sum( question_actual_result )/ NULLIF( count( 1 ), 0 )) * 100, 2 ), 0.00 ) AS homeworkCurrentRate, CASE WHEN homeworkCurrentRate = 100 THEN 'A' WHEN homeworkCurrentRate >= 80 AND homeworkCurrentRate < 100 THEN 'B' WHEN homeworkCurrentRate >= 60 AND homeworkCurrentRate < 80 THEN 'C' WHEN homeworkCurrentRate < 60 THEN 'D' END FROM zhzy_homework_homework_student_question zhhsq WHERE student_id = 490 AND question_actual_result IS NOT NULL AND homework_type = 1 AND update_time BETWEEN "2023-07-15" AND "2023-07-16" GROUP BY homework_id > 1054 - Unknown column 'homeworkCurrentRate' in 'field list' 如何修改
时间: 2024-04-21 11:23:30 浏览: 106
要解决这个问题,你需要做两个步骤:
1. 将 `homeworkCurrentRate` 移动到 `HAVING` 子句中,因为它是在 `SELECT` 子句中定义的别名。
2. 将 `CASE` 语句移动到外部查询中,以便你可以在 `SELECT` 子句中引用它。
下面是修改后的查询语句:
```sql
SELECT
finishHomeworkCount,
homeworkTitleCount,
homeworkCurrentRate,
CASE
WHEN homeworkCurrentRate = 100 THEN 'A'
WHEN homeworkCurrentRate >= 80 AND homeworkCurrentRate < 100 THEN 'B'
WHEN homeworkCurrentRate >= 60 AND homeworkCurrentRate < 80 THEN 'C'
WHEN homeworkCurrentRate < 60 THEN 'D'
END AS grade
FROM (
SELECT
COUNT(DISTINCT homework_id) AS finishHomeworkCount,
COUNT(1) AS homeworkTitleCount,
COALESCE(ROUND((SUM(question_actual_result) / NULLIF(COUNT(1), 0)) * 100, 2), 0.00) AS homeworkCurrentRate
FROM
zhzy_homework_homework_student_question zhhsq
WHERE
student_id = 490
AND question_actual_result IS NOT NULL
AND homework_type = 1
AND update_time BETWEEN "2023-07-15" AND "2023-07-16"
GROUP BY
homework_id
HAVING
homeworkCurrentRate IS NOT NULL
) AS subquery;
```
这样修改后的查询将会计算出每个作业的完成数量、作业标题数量以及作业的当前进度百分比,并根据百分比给出一个等级。
阅读全文