SQL 不允许直接聚合聚合。
但是,如果您在 FROM 子句中的子查询中编写内部聚合(或使用 WITH 子句和公共表表达式,CTE),则可以实现结果:
SELECT gc1.CategoryName, gc1.SubCategoryName, gc1.BookCount
FROM (SELECT cat.CategoryName, sb.SubCategoryName,
COUNT(bs.BookID) AS BookCount
FROM Category AS cat
JOIN SubCategory AS sb ON cat.CategoryID = sb.CategoryID
JOIN Book_Subcategory AS bs ON sb.SubCategoryID = bs.SubCategoryID
GROUP BY cat.CategoryName, sb.SubCategoryName
) AS gc1
WHERE gc1.BookCount = (SELECT MAX(gc2.BookCount)
FROM (SELECT cat.CategoryName, sb.SubCategoryName,
COUNT(bs.BookID) AS BookCount
FROM Category AS cat
JOIN SubCategory AS sb
ON cat.CategoryID = sb.CategoryID
JOIN Book_Subcategory AS bs
ON sb.SubCategoryID = bs.SubCategoryID
GROUP BY cat.CategoryName, sb.SubCategoryName
) AS gc2
)
这很复杂,因为它不使用 CTE,并且有一个必须写出两次的公用表表达式。
使用 CTE 形式(可能有语法错误):
WITH gc1 AS (SELECT cat.CategoryName, sb.SubCategoryName,
COUNT(bs.BookID) AS BookCount
FROM Category AS cat
JOIN SubCategory AS sb
ON cat.CategoryID = sb.CategoryID
JOIN Book_Subcategory AS bs
ON sb.SubCategoryID = bs.SubCategoryID
GROUP BY cat.CategoryName, sb.SubCategoryName
)
SELECT gc1.CategoryName, gc1.SubCategoryName, gc1.BookCount
FROM gc1
WHERE gc1.BookCount = SELECT MAX(gc1.BookCount) FROM gc1);
整齐多了!
如果您的 DBMS 可以轻松创建临时表,则可以使用临时表模拟 CTE。例如,IBM Informix Dynamic Server 可以使用:
SELECT cat.CategoryName, sb.SubCategoryName,
COUNT(bs.BookID) AS BookCount
FROM Category AS cat
JOIN SubCategory AS sb ON cat.CategoryID = sb.CategoryID
JOIN Book_Subcategory AS bs ON sb.SubCategoryID = bs.SubCategoryID
GROUP BY cat.CategoryName, sb.SubCategoryName
INTO TEMP gc1;
SELECT gc1.CategoryName, gc1.SubCategoryName, gc1.BookCount
FROM gc1
WHERE gc1.BookCount = (SELECT MAX(gc1.BookCount) FROM gc1);
DROP TABLE gc1; -- Optional: table will be deleted at end of session anyway
给定以下表格和数据,主查询(从这个答案复制和粘贴)给出了我在 MacOS X 10.6.4 上针对 IBM Informix Dynamic Server 11.50.FC6 运行时所期望的结果,即:
Non-Fiction SQL Theory 4
Fiction War 4
这并不能证明它在针对 Oracle 运行时“必须工作”——我没有 Oracle,也无法证明任何一种方式。它确实表明至少有一个 SQL DBMS 可以毫无问题地处理查询。(由于 IDS 不支持 WITH 子句和 CTE,我无法说明该公式是否有效。)
架构
CREATE TABLE Category
(
CategoryID INTEGER NOT NULL PRIMARY KEY,
CategoryName VARCHAR(20) NOT NULL
);
CREATE TABLE SubCategory
(
CategoryID INTEGER NOT NULL REFERENCES Category,
SubCategoryID INTEGER NOT NULL PRIMARY KEY,
SubCategoryName VARCHAR(20) NOT NULL
);
CREATE TABLE Book_SubCategory
(
SubCategoryID INTEGER NOT NULL REFERENCES SubCategory,
BookID INTEGER NOT NULL PRIMARY KEY
);
数据
INSERT INTO Category VALUES(1, 'Fiction');
INSERT INTO Category VALUES(2, 'Non-Fiction');
INSERT INTO SubCategory VALUES(2, 1, 'SQL Theory');
INSERT INTO SubCategory VALUES(2, 2, 'Mathematics');
INSERT INTO SubCategory VALUES(1, 3, 'Romance');
INSERT INTO SubCategory VALUES(1, 4, 'War');
INSERT INTO Book_SubCategory VALUES(1, 10);
INSERT INTO Book_SubCategory VALUES(2, 11);
INSERT INTO Book_SubCategory VALUES(3, 12);
INSERT INTO Book_SubCategory VALUES(3, 13);
INSERT INTO Book_SubCategory VALUES(4, 14);
INSERT INTO Book_SubCategory VALUES(1, 15);
INSERT INTO Book_SubCategory VALUES(1, 16);
INSERT INTO Book_SubCategory VALUES(2, 17);
INSERT INTO Book_SubCategory VALUES(1, 18);
INSERT INTO Book_SubCategory VALUES(3, 19);
INSERT INTO Book_SubCategory VALUES(4, 20);
INSERT INTO Book_SubCategory VALUES(4, 21);
INSERT INTO Book_SubCategory VALUES(4, 22);