【问题标题】:how to use upper query table alias in inner query in subquery如何在子查询的内部查询中使用上查询表别名
【发布时间】:2017-09-25 04:01:13
【问题描述】:
select upp.item_total,
   (select sum(iva.total_item_value_afs)
    from (select sum(item_value_afs) total_item_value_afs 
          from (select distinct inn.reg_no,inn.tpt_cuo_nam, inn.item_total, inn.item_value_afs
                from sigtasad.customs_import_data inn
                where inn.reg_no = upp.reg_no and inn.tpt_cuo_nam = upp.tpt_cuo_nam
               )
         ) iva
   ) total_item_value,
   sum(upp.code_tax_amount),
   upp.cmp_nam from SIGTASAD.CUSTOMS_IMPORT_DATA upp where upp.reg_no = '38699' and upp.company_tin = '9003247336' group by upp.reg_no, upp.tpt_cuo_nam, upp.cmp_nam, upp.item_total ;

此查询产生以下错误:

ORA-00904:“UPP”。“TPT_CUO_NAM”:无效标识符 00904. 00000 - "%s: 无效标识符"

【问题讨论】:

  • 作为你的问题,它无法做到。让我们试着解释一下你想做什么?因为你的代码不清楚。我的猜测是,您只想将 total_item_value 的列与 group by 2 列相加,但您想对其他列与 group by 4 列相加。对吗?
  • 今日小贴士:表别名!

标签: sql database oracle subquery table-alias


【解决方案1】:

尝试加入“派生表”,而不是使用“选择不同”的复杂“相关子查询”。没有任何样本数据等。这是一种猜测,但它可能看起来更像这样:

SELECT
      upp.reg_no
    , upp.tpt_cuo_nam
    , upp.cmp_nam
    , upp.item_total
    , d.total_item_value
    , SUM(upp.code_tax_amount)
FROM sigtasad.customs_import_data upp
LEFT JOIN (
            SELECT
                  inn.reg_no
                , inn.tpt_cuo_nam
                , SUM(iva.total_item_value_afs) total_item_value
            FROM sigtasad.customs_import_data inn
            GROUP BY
                  inn.reg_no
                , inn.tpt_cuo_nam
            ) d ON upp.reg_no = d.reg_no
                  AND upp.tpt_cuo_nam = d.tpt_cuo_nam
WHERE upp.reg_no = '38699'
AND upp.company_tin = '9003247336'
GROUP BY
      upp.reg_no
    , upp.tpt_cuo_nam
    , upp.cmp_nam
    , upp.item_total
    , d.total_item_value
;

【讨论】:

  • 没问题。干杯。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多