Sql 如何在子查询的内部查询中使用上层查询表别名
此查询生成以下错误: ORA-00904:“UPP”。“TPT\u CUO\u NAM”:无效标识符 90400000-%s:无效标识符 尝试连接“派生表”,而不是使用“select distinct”的复杂“相关子查询”。没有任何样本数据等。这有点像猜测,但看起来可能更像这样:Sql 如何在子查询的内部查询中使用上层查询表别名,sql,database,oracle,subquery,table-alias,Sql,Database,Oracle,Subquery,Table Alias,此查询生成以下错误: ORA-00904:“UPP”。“TPT\u CUO\u NAM”:无效标识符 90400000-%s:无效标识符 尝试连接“派生表”,而不是使用“select distinct”的复杂“相关子查询”。没有任何样本数据等。这有点像猜测,但看起来可能更像这样: select upp.item_total, (select sum(iva.total_item_value_afs) from (select sum(item_value_afs) total_it
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 ;
作为你的问题,这是做不到的。让我们试着解释一下你想做什么?因为你的代码不清楚。我的猜测是,您只希望对total_item_value的列使用group by 2列求和,但对其他列使用group by 4列求和。是吗?今天的提示:表格别名!没问题。干杯
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
;