这个问题是由于Amazon Redshift的限制造成的,这是基于Paraccel的柱状分析数据库。其中一个不受支持的功能是references in a GROUP BY column to the results of a correlated subquery。
例如,以下语句将生成红移错误,因为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的以下示例是另一个不受支持的用例(对生成此一般问题的specific question的回答)。
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模式?
发布于 2014-01-04 01:04:22
左连接应该可以解决这个问题,除非我遗漏了什么。
SELECT listing.listid
,COUNT(sales.listid)
FROM listing
LEFT JOIN sales
ON sales.listid = listing.listid
GROUP BY listing.listid
ORDER BY COUNT(sales.listid) DESC
; https://stackoverflow.com/questions/20821214
复制相似问题