如何在子查询中计数

时间:2015-01-11 03:34:31

标签: sql oracle oracle-apex

所以这就是我的代码。

SELECT TestName
FROM Testtype
WHERE TestID IN (Select TestID
                    from Tests
                    where Testno IN (Select Testno
                                      from Users
                                      WHERE StudentType= 'StudentType1'
                                      )
                    )

这会生成一个与Student类型匹配的测试名称列表

Test1
Test2

我想要,所以我可以计算test1有多少匹配,所以输出就像

No of student that undertook the test   Test Type
52                                      Test1
23                                      Test2

感谢任何帮助,谢谢。

2 个答案:

答案 0 :(得分:2)

用连接替换子查询,并添加GROUP BY

SELECT tt.TestName, COUNT(*) as NoOfStudents
FROM TestType tt
JOIN Tests t ON tt.TestID=t.TestID 
JOIN Users u ON t.Testno=u.Testno AND u.StudentType= 'StudentType1'
GROUP BY tt.TestName

答案 1 :(得分:0)

您也可以使用此查询。

SELECT TestName,count(*) StudentsCount
FROM Testtype tt,
     Tests t,
     Users usr
where usr.StudentType='StudentType1'
  and usr.Testno = t.Testno
  and t.TestID=tt.TestID
group by TestName