是否可以在Redshift数据库中查询多个模式并组合结果?
就像这样:
SELECT DISTINCT name, SUM(usage) AS totalUsage
FROM schemaOne.terms, schemaTwo.terms将引发错误:
亚马逊无效操作:列引用“名称”不明确;
那么,这是否可能,这是否一种良好的做法?我想为每个客户端创建一个模式,它将拥有自己的术语(标记)表和其他表。
,例如
SchemaOne:术语
id, name, description, usage
_______________________________
1 css CSS is bla. 14
2 html HTML rocks! 9SchemaTwo:术语
id, name, description, usage
_______________________________
1 css CSS is cool. 8
2 other x 4现在,我想将结果结合起来,可以输出如下内容:
name totalUsage
_________________
css 22
html 9
other 4发布于 2016-05-26 08:05:26
首先在派生表中执行UNION ALL,然后应用GROUP BY
select name, sum(usage) as totalUsage
from
(
select name, usage
from SchemaOne.terms
union all
select name, usage
from SchemaTwo.terms
) dt
group by name请注意,有些产品将name和usage作为保留字,因此您可能需要将它们分隔为"name"和/或"usage"。
也许性能更好的版本:
select name, sum(usage) as totalUsage
from
(
select name, sum(usage) as usage
from SchemaOne.terms
group by name
union all
select name, sum(usage) as usage
from SchemaTwo.terms
group by name
) dt
group by name原始答案
如果同一列名存在于多个表中,则必须限定该列,即将表名放在列名之前:
SELECT DISTINCT schemaOne.terms.name, SUM(usage) as totalUsage
FROM schemaOne.terms, schemaTwo.terms表别名很方便:
SELECT DISTINCT s1t.name, SUM(usage) as totalUsage
FROM schemaOne.terms s1t, schemaTwo.terms s2thttps://stackoverflow.com/questions/37454840
复制相似问题