You need to enable JavaScript to run this app. AWS Redshift执行左连接时报left附近语法错误问题求助
优惠活动
大模型
产品
解决方案
定价
更多

AWS Redshift执行左连接时报left附近语法错误问题求助

问题根因定位

核心错误为SQL子句书写顺序不符合Redshift语法解析规则:

  • 按照SQL标准执行逻辑,书写顺序需遵循 FROM 主表 → JOIN 关联表/子查询 + ON关联条件 → WHERE 数据过滤 的固定顺序
  • 当前代码中,WHERE 过滤逻辑被放在了 LEFT JOIN 关联操作之前,数据库解析到left关键字时,已经判定前面的查询块为完整的“主表+过滤条件”结构,无法识别后续的join语法,因此抛出 syntax error at or near "left" 的报错。
修正后可直接执行的SQL
select upper(contract_id) as tdr_contract_no, 
       quantity_sold, 
       upper(ordernbrpiid) as tdr_agcy_piid, 
       total_price, 
       sin_number as tdr_sin, 
       desc_deliverable,
       price_per_unit, 
       name as tdr_name, 
       Part_number as tdr_part_no, 
       unit_measure, 
       date_created
from tdr.order_details as apple
left join (
    select REPLACE(UPPER(TRIM(REPLACE(regexp_substr(contractnumber, '.*_'), '_', ''))), '-', '') as catalog_contract_no,
           contractnumber, 
           vendorname, 
           vendorPartNumber as catalog_pt_no, 
           manufacturername as oem_name, 
           uniqueitemidentifier,
           sin as catalog_sin, 
           standardizedProductName, 
           standardizedProductDescription, 
           catalogmedianprice
    from edw.fasdata_source.xsb_src_enriched_stg
) as catalog1
on upper(apple.contract_id) = catalog1.catalog_contract_no 
and apple.Part_number = catalog1.catalog_pt_no
Where sin_number in ('132 8STLOC', '132 8RC', '132 8', '132 34STLOC', '132 34', '132 34RC',
'132 33STLOC', '132 33RC', '132 33', '132 32STLOC', '132 32RC', '132 32', '33411', '33411RC',
'33411STLOC', '33411REF', '511210RC', '511210STLOC',
'511210') 
AND date_created between '2021-01-01' and '2021-03-31';
补充说明
  • 上述修正同时调整了ON条件里的别名引用:原代码在ON条件中引用了SELECT阶段定义的别名(apple.tdr_contract_no、apple.tdr_part_no),虽然Redshift部分版本支持跨子句引用SELECT别名,但为了规避版本兼容问题,直接在ON条件中使用原始字段计算表达式更稳妥。
  • 本次语法报错和表结构、函数逻辑、关联条件本身没有关系,纯粹是子句顺序错位导致的解析失败。

内容的提问来源于stack exchange,提问作者mbs1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.24 06:15:09