如何在Presto SQL中退出联接?

时间:2018-07-19 14:34:51

标签: presto

即使阅读了文档,也无法终生在Presto中找到简单的左联接。我对Postgres非常熟悉,并在那儿测试了我的查询,以确保我没有出现明显的错误。请参考下面的代码:

select * from

(select cast(order_date as date),   
        count(distinct(source_order_id)) as prim_orders, 
        sum(quantity) as prim_tickets, 
        sum(sale_amount) as prim_revenue 
from table_a
where order_date >= date '2018-01-01'
group by 1)

left join

(select summary_date, 
        sum(impressions) as sem_impressions, 
        sum(clicks) as sem_clicks, 
        sum(spend) as sem_spend,  
        sum(total_orders) as sem_orders, 
        sum(total_tickets) as sem_tickets, 
        sum(total_revenue) as sem_revenue 
from table_b
where site like '%SEM%'
and summary_date >= date '2018-01-01'
group by 1) as b

on a.order_date = b.summary_date

运行时出现以下错误

SQL Error: Failed to run query
  Failed to run query
    line 1:1: mismatched input 'on' expecting {'(', 'SELECT', 'DESC', 'WITH', 
'VALUES', 'CREATE', 'TABLE', 'INSERT', 'DELETE', 'DESCRIBE', 'GRANT', 
'REVOKE', 'EXPLAIN', 'SHOW', 'USE', 'DROP', 'ALTER', 'SET', 'RESET', 'START', 'COMMIT', 'ROLLBACK', 'CALL', 'PREPARE', 'DEALLOCATE', 'EXECUTE'} (Service: AmazonAthena; Status Code: 400; Error Code: InvalidRequestException; Request ID: a33a6671-07a2-4d7b-bb75-f70f7b82409e)
line 1:1: mismatched input 'on' expecting {'(', 'SELECT', 'DESC', 'WITH', 'VALUES', 'CREATE', 'TABLE', 'INSERT', 'DELETE', 'DESCRIBE', 'GRANT', 'REVOKE', 'EXPLAIN', 'SHOW', 'USE', 'DROP', 'ALTER', 'SET', 'RESET', 'START', 'COMMIT', 'ROLLBACK', 'CALL', 'PREPARE', 'DEALLOCATE', 'EXECUTE'} (Service: AmazonAthena; Status Code: 400; Error Code: InvalidRequestException; Request ID: a33a6671-07a2-4d7b-bb75-f70f7b82409e)

2 个答案:

答案 0 :(得分:1)

我注意到的第一个问题是您的join子句假定第一个子查询的别名为a,但根本没有别名。我建议为该表加上别名,以查看是否可以解决该问题(我也建议为order_date语句之外的cast()列添加别名,因为您要加入该列)。

尝试一下:

select * from

(select cast(order_date as date) as order_date,   
        count(distinct(source_order_id)) as prim_orders, 
        sum(quantity) as prim_tickets, 
        sum(sale_amount) as prim_revenue 
from table_a
where order_date >= date '2018-01-01'
group by 1) as a

left join

(select summary_date, 
        sum(impressions) as sem_impressions, 
        sum(clicks) as sem_clicks, 
        sum(spend) as sem_spend,  
        sum(total_orders) as sem_orders, 
        sum(total_tickets) as sem_tickets, 
        sum(total_revenue) as sem_revenue 
from table_b
where site like '%SEM%'
and summary_date >= date '2018-01-01'
group by 1) as b

on a.order_date = b.summary_date

答案 1 :(得分:0)

一种选择是使用with声明子查询:

with a as 
  (select cast(order_date as date),   
    count(distinct(source_order_id)) as prim_orders, 
    sum(quantity) as prim_tickets, 
    sum(sale_amount) as prim_revenue 
  from table_a
  where order_date >= date '2018-01-01'
  group by 1),

b as 
  (select summary_date, 
    sum(impressions) as sem_impressions, 
    sum(clicks) as sem_clicks, 
    sum(spend) as sem_spend,  
    sum(total_orders) as sem_orders, 
    sum(total_tickets) as sem_tickets, 
    sum(total_revenue) as sem_revenue 
  from table_b
  where site like '%SEM%'
  and summary_date >= date '2018-01-01'
  group by 1)

select * from a
left join b
on a.order_date = b.summary_date;