【问题标题】:table alias not work in subquery in oracle表别名在 oracle 的子查询中不起作用
【发布时间】:2017-09-24 10:54:15
【问题描述】:

我正在使用 sum 聚合函数和子查询生成记录,但别名在内部查询中不起作用。 我的查询是

    select UPP.item_total, 
           (select sum(INN.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))    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": 标识符无效

我想要这样的结果!!!

【问题讨论】:

  • 您应该编辑您的问题并提供示例数据和逻辑解释。
  • 不确定您要实现的目标,但您缺少最内层子选择的别名
  • 上层查询生成此错误:ORA-00904: "UPP"."TPT_CUO_NAM": invalid identifier that is my problem @ulferts

标签: sql oracle select subquery alias


【解决方案1】:

您的查询有很多错误和不良习惯。例如:

  • 您使用未定义的表别名来限定列名。
  • 您正在按不在 select 中的列进行聚合。
  • 您在具有sum() 的子查询上使用sum()

根据您展示的图片,您可能想要这样的东西:

select upp.item_total,
       sum(item_value_afs) as 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.cmp_nam, upp.item_total ;

或许:

select upp.item_total,
       sum(sum(item_value_afs)) over (partition by upp.cmp_nam, upp.item_total) as 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.cmp_nam, upp.item_total ;

【讨论】:

  • 我使用子查询只是因为item_value_afs中的重复值,您编写的查询将所有这些值相加。
【解决方案2】:

你最里面的子查询

(select distinct nn.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
)

引用未连接的表 (upp)。它也没有别名,但这个问题稍后会出现。请注意,似乎还有一个类型 nn.reg_no 而不是 inn.reg_no

此处未显示表格的结构,但解决问题将意味着:

(select distinct inn.reg_no,inn.tpt_cuo_nam, inn.item_total, inn.item_value_afs
 from sigtasad.customs_import_data inn, SIGTASAD.CUSTOMS_IMPORT_DATA upp
 where inn.reg_no = upp.reg_no and inn.tpt_cuo_nam = upp.tpt_cuo_nam
)

【讨论】:

  • 我现在就用!
  • 不不,兄弟!两张是同一张桌子! sigtasad.customs_import_data 客栈,SIGTASAD.CUSTOMS_IMPORT_DATA upp
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多