2017-09-24 31 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

我現在就用它! –