结果是tony和alex,他们的书出现在两种不同的类型中。汤姆出现在三种类型中,所以不是结果。如何编写查询语句来实现呢?感谢。
答案 0 :(得分:0)
要仅按作者匹配问题组中显示的输出,并使用having子句过滤聚合。
SELECT
A.author
, COUNT(DISTINCT I."Type")
FROM authorCollection A
INNER JOIN book I ON A.key = I.key
AND I."TYPE" <> 'UNKNOWN'
GROUP BY
A.author
HAVING COUNT(DISTINCT I."Type") = 2
要计算符合该条件的作者数量(随后询问),请将上面的查询设为“派生表”,如下所示:
SELECT
COUNT(*) as num_of_authors
FROM (
SELECT
A.author
FROM authorCollection A
INNER JOIN book I ON A.key = I.key
AND I."TYPE" <> 'UNKNOWN'
GROUP BY
A.author
HAVING COUNT(DISTINCT I."Type") = 2
) AS derived
注意我删除了派生表中的第二列,因为结果不需要它,但是请注意派生表查询的having子句仍然可以像以前一样工作。