2017-09-25 112 views
-1
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": invalid identifier 00904. 00000 - "%s: invalid identifier"

+0

作爲你的問題,它無法做到。讓我們試着解釋你想做什麼?因爲你的代碼不清楚。我的猜測是,你只想爲total_item_value的列添加2列的總和,但是你想爲其他列添加4列的總和。那就對了? –

+0

今天提示:表別名! – jarlh

回答

0

嘗試加入一個「衍生的表」而不是了採用複雜的「相關子查詢」,「選擇不同」。沒有任何樣本數據等,這是猜測,但它可能看起來更像這樣:

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 
; 
+0

感謝Bro確切的答案! @Used_By_Already –

+0

沒問題。乾杯。 –