【问题标题】:joining on a query result加入查询结果
【发布时间】:2018-11-22 04:37:12
【问题描述】:

我是 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' 关键字之间有一些内容

【问题讨论】:

  • 当您查看语法摘要时,您所写内容的理由是什么?显示问题的minimal reproducible example 是什么? (修辞。)
  • 样本数据和期望的结果将真正帮助其他人理解您正在尝试做的事情。

标签: sql join teradata


【解决方案1】:

SQLselect 语法是

[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')

【讨论】:

  • 很高兴为您提供帮助,如果对您有帮助,您可以标记此答案
  • @u356877 如果对您有帮助,您可以标记此答案
【解决方案2】:

您似乎希望 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
             );

【讨论】:

    猜你喜欢
    • 2013-12-14
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 2021-08-06
    • 1970-01-01
    • 2013-04-16
    • 2014-12-28
    • 1970-01-01
    相关资源
    最近更新 更多