2017-09-24 34 views
0

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

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”:无效的标识符

我想是这样的结果!

enter image description here

+0

您应该编辑您的问题并提供样本数据和逻辑解释。 –

+0

不知道你想达到什么,但你缺少最内层子查询的别名 – ulferts

+0

上面的查询生成此错误:ORA-00904:“UPP”。“TPT_CUO_NAM”:无效标识符 这是我的问题 @ulferts –

回答

0

你的查询有许多错误和不良的生活习惯。例如:

  • 您使用未定义的表别名限定列名。
  • 您按照不在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 ; 
+0

我使用子查询的原因是item_value_afs中的重复值, ,其中您所编写的查询将所有这些值相加。 –

0

你最里面的子查询

(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 
) 
+0

如何解决? –

+0

@EngMohammadJalalAhmadzai我更新了anwer – ulferts

+0

我现在就用它! –