子查询有子句错误,我错过了什么?

时间:2011-09-28 04:49:46

标签: sql database sql-server-2005

为什么我收到此错误?

 SELECT        unitCode
        FROM            Enrolment
        WHERE        count(studentID) >
         (SELECT        AVG(Students) AS avgstudents
        FROM            (SELECT        COUNT(*) AS Students
       FROM            Enrolment AS Enrolment_1
        GROUP BY unitCode) AS a))

4 个答案:

答案 0 :(得分:1)

看起来最后有一个额外的paren。我不太了解SQL,所以我不能说是否有任何错误(或者paren是否是一个问题)。

答案 1 :(得分:1)

在where子句中,我们不能使用聚合函数。您可以在HAVING子句中使用count(studentID)函数。

检查此样本,

SELECT department, SUM(sales) as "Total sales"
FROM order_details
GROUP BY department
HAVING SUM(sales) > 1000;

答案 2 :(得分:0)

大卫检查一下,

select count(*),unicode from Enrolement 
    having count(StudentID) > (select avg(Students) ad avgstudents,unitCode
    from table_Name where Enrolement = "some of ur condtion"
    group by unitCode) 

需要更改格式化语法的查询..相应地检查您的查询业务。

答案 3 :(得分:0)

试试这个:

select unitCode
from Enrolment
group by unitCode
having count(*) > (select avg(c.c) from (select count(*) as c from Enrolment group by unitCode) c);

适当尊重:

TSQL: Cannot perform an aggregate function AVG on COUNT(*) to find busiest hours of day