Example of Using Aggregation on Nested Tables
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
Explanation
- The WITH clause, occupying the first nine lines of the example, illustrates SQL's counterpart of Tutorial D's construct of the same name. It assigns the name, C_ER, to the result of Example. That name, C_ER, is then used in the FROM clause of the expression that follows the WITH clause. Note that here the name comes before AS and the expression defining it comes after. This is consistent with the analogous use of AS in CREATE VIEW statements.
- TABLE(ER(ExamResult)) seems to be the only way of having a multiset valued column operated on as an element of a FROM clause-a simple column name is not allowed to appear here. TABLE(ExamResult) can't be used either, because when an invocation of TABLE appears as a FROM clause element, its operand is required to be, specifically, an invocation of a user-defined function. Assumed here ER defined like:
CREATE FUNCTION ER
(SM ROW ( StudentId SID, Mark INTEGER ) MULTISET )
RETURNS TABLE (StudentId SID, Mark INTEGER)
RETURN SM;
The type name TABLE ( StudentId SID, Mark INTEGER ) is actually just a synonym for ROW (StudentId SID, Mark INTEGER ) MULTISET). The misleading synonym is available only in a RETURNS clause and not as a parameter type, for example. So ER is actually a no-op, returning its input.
- (SELECT COUNT(*) FROM TABLE(ER(ExamResult)) AS t) is a scalar subquery, yielding the cardinality of the multiset of rows that is the value of the column ExamResult in the current row of C_ER. Because we are using the expression to denote a scalar value rather than a table, naming the column would be pointless (apart, perhaps, from injecting a somewhat sarcastic element of purism). As COUNT(*) doesn't use a column name, Example is valid even if we omit the invocation of CAST to assign column names.
- AS n then gives the resulting column the name n. Note that here the name comes after AS and the expression defining it comes before, in the same style as the use of AS to define the range variables C and EM in the example.