查询另一个查询的结果

时间:2016-04-07 10:21:14

标签: sql

我希望再次查询括号中的查询结果。我在堆栈中引用了一些帖子,并提出了以下查询。但不知何故,第二个查询(括号外的查询)无法正常工作。错误表示第二个'附近的语法不正确。有人可以请帮助。

select CourseID, count(CourseID)
from
    (select CustomisationID, CourseID , Count(CourseID)
     from tblFilter
     where CustomisationID in (43, 51) 
     group by CourseID, CustomisationID) 
group by CourseID

2 个答案:

答案 0 :(得分:1)

Try this

select M.CourseID, count(M.CourseID) from
(
   select CustomisationID, CourseID , Count(CourseID) AS Cnt from tblFilter where CustomisationID in (43,51) 
    group by CourseID, CustomisationID
)M 
group by M.CourseID

答案 1 :(得分:0)

You must set alias for subquery. Try Something like this:

select CourseID, count(CourseID) 
from
(
    select CustomisationID, CourseID , Count(CourseID) AS Cnt
    from tblFilter 
    where CustomisationID in (43,51) 
    group by CourseID, CustomisationID
) AS TMP
group by CourseID