SELECT uid,exam_id,ROUND(AVG(score),0) avg_score FROM exam_record WHERE score IS NOT NULL AND uid IN(SELECT uid FROM user_info WHERE nick_name LIKE '牛客[0-9]%号' or (ISNUMERIC(nick_name)=1) AND exam_id IN (SELECT exam_id FROM examination_info WHERE LOWER(.tag) like 'c%' ) GROUP BY uid,exam_id ORDER BY uid,AVG(score) 除非另外还指定了 TOP、OFFSET 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效。
时间: 2024-04-14 20:26:10 浏览: 73
这个错误提示是因为在一个子查询中使用了ORDER BY子句。在子查询中,ORDER BY子句是无效的,除非还指定了TOP、OFFSET或FOR XML。
要解决这个问题,你可以将子查询的结果存储在临时表或表变量中,并在外部查询中使用ORDER BY子句。具体的解决方法如下所示:
```
-- 创建临时表存储子查询的结果
CREATE TABLE #temp (
uid INT,
exam_id INT,
avg_score DECIMAL(10, 2)
)
-- 将子查询的结果插入临时表中
INSERT INTO #temp (uid, exam_id, avg_score)
SELECT uid, exam_id, ROUND(AVG(score), 0) avg_score
FROM exam_record
WHERE score IS NOT NULL
AND uid IN (
SELECT uid
FROM user_info
WHERE nick_name LIKE '牛客[0-9]%号' OR ISNUMERIC(nick_name) = 1
)
AND exam_id IN (
SELECT exam_id
FROM examination_info
WHERE LOWER(tag) LIKE 'c%'
)
GROUP BY uid, exam_id
-- 对临时表中的结果进行排序并返回
SELECT uid, exam_id, avg_score
FROM #temp
ORDER BY uid, avg_score
-- 删除临时表
DROP TABLE #temp
```
通过将子查询的结果存储在临时表中,并在外部查询中使用ORDER BY子句,你就可以解决这个问题了。请注意,你可能需要根据实际情况调整临时表的定义和数据类型。
阅读全文