感谢@dnoeth,我找到了解决方案。
SELECT T2.first_name T2.last_name ,SUM(t1.Cnt) FROM myTable AS T2 LEFT JOIN ( SELECT first_name, last_name, Count(*) AS Cnt FROM myTable GROUP BY 1, 2 ) AS T1 ON T1.first_name = T2.last_name AND T1.last_name = T2.first_name GROUP BY 1, 2
你在谈论一个 标量子查询 在选择?
SELECT last_name ,( SELECT Count(*) FROM myTable AS T1 WHERE T1.FIRST_NAME = T2.LAST_NAME ) FROM myTable AS t2
那么你是对的,你不能把它重写为OLAP函数。
那些Scalar子查询往往表现不好,但通常可以使用外部联接重写它们:
SELECT t2.last_name ,t1.Cnt FROM myTable AS t2 LEFT JOIN ( SELECT first_name, Count(*) AS Cnt FROM myTable GROUP BY 1 ) AS t1 ON T1.FIRST_NAME = T2.LAST_NAME