这个问题是由于 Amazon Redshift(基于 Paraccel 的列式分析数据库)的限制而出现的。不支持的功能之一是 GROUP BY 列中对相关子查询结果的引用。
例如,以下语句将生成 Redshift 错误,因为 GROUP BY 使用list
由子查询生成的。
select listing.listid,
(select count (sales.listid) from sales where sales.listid=listing.listid) as list
from listing
group by list, listing.listid;
以下来自Gordon Linoff的示例是另一个不受支持的用例(对生成此一般问题的特定问题的回答)。
select type, (case when cnt > XXX then url end) as url, sum(cnt) as visit_cnt
from (select type, url, count(*) as cnt
from t
group by type, url
) t
group by type, url
order by type, sum(cnt) desc;
这个问题的目的是找到一个通用模式来克服这个特定的 Amazon Redshift 相关子查询限制。有哪些替代 SQL 模式可以实现与使用相关子查询中的值相同的结果?