加入查询结果

时间:2018-06-12 17:45:58

标签: sql join teradata

我是sql的新手,无法使用现有表加入查询结果表。我一直在尝试将查询结果命名为 res_tab但它似乎不起作用。我只是希望能够将查询结果与现有表连接起来。这是我到目前为止所拥有的:

(select distinct op_id
from cmpr_dept_vmdb.cust_promotion
where promo_id in ('TB4M40', 'TB4M41', 'TB4M42')
and regstrn_status_cd = 'R') as res_tab;

select elite_hist.op_id
from cmpr_dept_vmdb.elite_hist_detail as elite_hist
where elite_hist.instant_elt_promo_cd in ('F1', 'F2', 'F3')
inner join elite_hist
on res_tab.op_id = elite_hist.op_id

它返回以下错误: 语法错误:预期在')'和'as'关键字

之间

2 个答案:

答案 0 :(得分:1)

SQL select语法是

[SELECT] ...
[FROM] .....
[JOIN] ....
[WHERE] ....
[GROUP BY] .....

您似乎想join喜欢这样。

select elite_hist.op_id
from cmpr_dept_vmdb.elite_hist_detail as elite_hist
inner join 
(
    select distinct op_id
    from cmpr_dept_vmdb.cust_promotion
    where promo_id in ('TB4M40', 'TB4M41', 'TB4M42')
    and regstrn_status_cd = 'R'
) as res_tab;
on res_tab.op_id = elite_hist.op_id
where elite_hist.instant_elt_promo_cd in ('F1', 'F2', 'F3')

答案 1 :(得分:0)

您似乎希望subquery正确关联方法

select distinct elite_hist.op_id
from cmpr_dept_vmdb.elite_hist_detail as elite_hist
where instant_elt_promo_cd in ('F1', 'F2', 'F3') and 
      exists (select 1 
              from cmpr_dept_vmdb.cust_promotion as res_tab
              where res_tab.op_id = elite_hist.op_id and
                    res_tab.instant_elt_promo_cd in ('F1', 'F2', 'F3') and
                    res_tab.regstrn_status_cd = 'R
             );