【SQL】ORACLE生成临时表

在日常的SQL查询中,我们需要对要查询的数据进行事先处理,然后再在预先处理好的数据里面进行查询。此时我们就需要用到临时表了,将数据预先处理好放到临时表里面,然后再在临时表里根据我们需要的条件进行查询。

假设我们有一个收入表 :incoming ,里面的字段分别为:id,name,amt,cur 我们要求每个人的amt换算成RMB之后的和。由于币种有区别,我们需要事先将amt对应币种按照汇率换算成人民币再进行计算。

另一个表时xrt表,存放每个币种对应人民币牌价。

有两种方法,一种是用with as方法,事先生成一个临时表temp,然后再在这个临时表里查。

with temp as
(
select name, amt*(select cnyrate from xrt x where x.cur=i.cur ) from incoming 

)
select name, sum(amt) from incoming group by name

  第二个方法是使用select方法

select name,sum(amt) from (select name,amt*(selct cnyrate from xrt x where x.cur=i.cur) from incoming) temp group by name;