【问题标题】:Joining two Athena tables with two where clauses使用两个 where 子句连接两个 Athena 表
【发布时间】:2019-08-23 04:50:31
【问题描述】:

我有两个带有以下查询的 Athena 表:

select 
  date,
  uid,
  logged_hrs,
  extract(hour from start_time) as hour
from schema.table1
where building = 'MKE'
  and pt_date between date '2019-01-01' and date '2019-01-09'

和

select 
    associate_uid as uid,
    date(substr(fcdate_utc, 1, 10)) as pt_date,
    learning_curve_level
  from tenure.learningcurve 
  where warehouse_id = 'MKE'
    and date(substr(fcdate_utc, 1, 10)) between date '2019-01-01' and date '2019-01-09'

我想加入他们的 uid 和 pt_date。我怎样才能做到这一点?

我试过了:

select (select 
      date,
      uid,
      logged_hrs,
      extract(hour from start_time) as hour
    from schema.table1
    where building = 'MKE'
      and pt_date between date '2019-01-01' and date '2019-01-09') as a
left join (select 
        associate_uid as uid,
        date(substr(fcdate_utc, 1, 10)) as pt_date,
        learning_curve_level
      from tenure.learningcurve 
      where warehouse_id = 'MKE'
        and date(substr(fcdate_utc, 1, 10)) between date '2019-01-01' and date '2019-01-09'
) as b 
on a.uid=b.uid and a.pt_date = b.pt_date

但是以上导致错误mismatched input 'left' expecting {<eof>, ',', 'from', 'where', 'group', 'order', 'having', 'limit', 'union', 'except', 'intersect'}

【问题讨论】:

    标签: sql amazon-athena presto


    【解决方案1】:

    任何 sql 中的连接语法如下所示

    Select <column list>
      from Table_1
         left/right/inner Join Table_2
         ON <join condition>
    

    table_1 和 table_2 可以是表或其他选择语句

    您缺少 select * from, 试试这个,我不能检查其他语法错误,但这是一般的想法

    select  a.*, b.*
      from  (select  date,
                     uid,
                     logged_hrs,
                     extract(hour from start_time) as hour
               from schema.table1
              where building = 'MKE'
                and pt_date between date '2019-01-01' and date '2019-01-09'
            ) as a
         left join 
           (select  associate_uid as uid,
                    date(substr(fcdate_utc, 1, 10)) as pt_date,
                    learning_curve_level
              from tenure.learningcurve 
             where warehouse_id = 'MKE'
               and date(substr(fcdate_utc, 1, 10)) between date '2019-01-01' and date '2019-01-09'
            ) as b 
          on a.uid=b.uid and a.pt_date = b.pt_date
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-05
      • 1970-01-01
      • 2023-04-10
      • 2021-08-04
      • 1970-01-01
      相关资源
      最近更新 更多