Using Aggregation on Nested Tables
Example is the most direct translation of its counterpart in the theory book that can be obtained in SQL but it is so over-elaborate that no SQL practitioner would consider using it. It uses the aggregate operator COUNT on the table values for column ExamResult to obtain the number of students who sat each exam. Unfortunately, as already noted, we cannot operate directly on ExamResult as a FROM clause element. Instead, we need to use an artifice that is specially devised for the sake of this example.
Example: How many students sat each exam
WITH C_ER AS (
SELECT CourseId,
CAST (
TABLE (SELECT DISTINCT StudentId, Mark
FROM EXAM_MARK AS EM
WHERE EM.CourseId = C.CourseId)
AS ROW ( StudentId SID, Mark INTEGER ) MULTISET)
AS ExamResult
FROM COURSE AS C)
SELECT CourseId, (SELECT COUNT (*)
FROM TABLE (ER (ExamResult)) AS t) AS n
FROM C_ER