1

这个问题是由于 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 模式可以实现与使用相关子查询中的值相同的结果?

4

1 回答 1

1

除非我遗漏了什么,否则左连接应该可以解决问题。

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
; 
于 2014-01-03T17:04:21.560 回答