Presto SQL - 两个完全不同的表连接平行而不是垂直
Posted
技术标签:
【中文标题】Presto SQL - 两个完全不同的表连接平行而不是垂直【英文标题】:Presto SQL - Two completely different tables to join parallel instead of vertical 【发布时间】:2021-11-06 04:18:43 【问题描述】:我有两张表,其中包含完整的数据集和标题。
我设法连接在一起,但使用下面的代码,但这是垂直组合。
QUERY = """
SELECT SUM(CAST(US.amount as DECIMAL(13,2))) filter (where type IN ('USA')) as "USA Net Amount" from da1.tb1
UNION
SELECT SUM(CAST(EU.amount as DECIMAL(13,2))) filter (where type IN ('EU')) as "EU Net Amount" from da2.tb2
"""
results = fetch_data(QUERY)
display(results)
results.to.csv('output.csv',index=False)
下面是我的输出,第二行是欧盟净额输出。
USA Net Amount |
---|
50.00 |
100.00 |
我想要实现的东西,欧元净额输出为新的第二列。
USA Net Amount | EU Net Amount |
---|---|
50.00 | 100.00 |
【问题讨论】:
也许是CROSS JOIN
代替?
或者只使用逗号 SELECT SUM(CAST(US.amount as DECIMAL(13,2))) 过滤器(其中输入 IN ('USA'))作为来自 da1 的“美国净金额”。 tb1 , SELECT SUM(CAST(EU.amount as DECIMAL(13,2))) 过滤器(其中输入 IN ('EU'))作为来自 da2.tb2 的“EU Net Amount”
@jarlh - 'CROSS' 的输入不匹配
@Emanuele - 这也不起作用
【参考方案1】:
select t1."USA Net Amount", t2."EU Net Amount"
FROM
(SELECT SUM(CAST(US.amount as DECIMAL(13,2))) filter (where type IN ('USA')) as "USA Net Amount"
from da1.tb1) t1
CROSS JOIN
(SELECT SUM(CAST(EU.amount as DECIMAL(13,2))) filter (where type IN ('EU')) as "EU Net Amount"
from da2.tb2) t2
不过,不知道这个语法是否支持?
符合 SQL-2016 使用了 SQL-2016 之外的以下功能: T612,高级 OLAP 操作 T611,基本 OLAP 操作 F591,派生表 F401,扩展连接表
【讨论】:
它必须是更好/优化的输出方式,好像有更多列,上面的脚本似乎非常重复【参考方案2】:在 UNION ALL 中的查询中添加类型列(这里需要 UNION ALL,而不是 UNION)来指示国家和使用条件聚合:
select max(AMT) FILTER(WHERE type="USA") as "USA Net Amount",
max(AMT) FILTER(WHERE type="EU") as "EU Net Amount"
from
(
--aggregation in union ALL can be executed in parallel
SELECT SUM(CAST(US.amount as DECIMAL(13,2))) as Amt, type from da1.tb1 where type IN ('USA') group by type
UNION ALL ---not UNION !
SELECT SUM(CAST(EU.amount as DECIMAL(13,2))) as Amt, type from da2.tb2 where type IN ('EU') group by type
) as s
或在 UNION ALL 之后使用单个聚合步骤:
select sum(AMT) FILTER(WHERE type="USA") as "USA Net Amount",
sum(AMT) FILTER(WHERE type="EU") as "EU Net Amount"
from
(
SELECT CAST(US.amount as DECIMAL(13,2)) as Amt, type from da1.tb1 where type IN ('USA')
UNION ALL ---not UNION !
SELECT CAST(EU.amount as DECIMAL(13,2)) as Amt, type from da2.tb2 where type IN ('EU')
) as s
【讨论】:
UNION ALL 也只做垂直输出 @Alex UNION 做 DISTINCT。 UNION ALL 将数据集合二为一,不应用 distict。 UNION = UNION ALL + DISTINCT。 UNION ALL 在完全并行模式下执行得更快,因为它没有 DISTINCT以上是关于Presto SQL - 两个完全不同的表连接平行而不是垂直的主要内容,如果未能解决你的问题,请参考以下文章