使用来自多个表的数据更新列

时间:2016-03-21 15:02:49

标签: sql oracle

我想更新表TOTAL_DURATIONIAM_RSP_INSTRUCTION列中的值,其中TOTAL_DURATION的值基于查询

select (paid_rsp + rd) as total from 
(
  select count (b.rsp_instruction_id) as paid_rsp, a.rsp_duration as rd,
    b.rsp_instruction_id
  from IAM_RSP_INSTRUCTION a
  JOIN IAM_BUY_FUND_INFO b on a.RSP_ID = b.RSP_INSTRUCTION_ID
  where b.RSP_INSTRUCTION_ID is not null
  and a.rsp_status= 'approved' or a.rsp_status='terminated' 
  group by a.rsp_duration, b.rsp_instruction_id, a.rsp_status<br>
  HAVING a.rsp_duration > -1
  order by b.rsp_instruction_id
) ,
and rsp_id from IAM_RSP_INSTRUCTION = rsp_instruction_id
from IAM_BUY_FUND_INFO

目前,我有一个更新查询:

UPDATE IAM_RSP_INSTRUCTION
SET j.TOTAL_DURATION = (
  select (paid_rsp + rd)  as total
  from (
    select count (b.rsp_instruction_id) as paid_rsp, a.rsp_duration as rd,
      b.rsp_instruction_id 
    from iam_rsp_instruction a
    JOIN iam_buy_fund_info b on a.RSP_ID = b.RSP_INSTRUCTION_ID
    where b.RSP_INSTRUCTION_ID is not null
    and a.rsp_status= 'approved' or a.rsp_status='terminated'
    group by a.rsp_duration, b.rsp_instruction_id, a.rsp_status
    HAVING a.rsp_duration > -1
    order by b.rsp_instruction_id
  )
  WHERE IAM_RSP_INSTRUCTION.rsp_id = rsp_instruction_id
);

当我运行查询时,8小时后,它仍在运行,没有记录更新。

注意:子查询在我运行时起作用了。

select (paid_rsp + rd) as total from 
    (
      select count (b.rsp_instruction_id) as paid_rsp, a.rsp_duration as rd,
        b.rsp_instruction_id
      from IAM_RSP_INSTRUCTION a
      JOIN IAM_BUY_FUND_INFO b on a.RSP_ID = b.RSP_INSTRUCTION_ID
      where b.RSP_INSTRUCTION_ID is not null
      and a.rsp_status= 'approved' or a.rsp_status='terminated' 
      group by a.rsp_duration, b.rsp_instruction_id, a.rsp_status<br>
      HAVING a.rsp_duration > -1
      order by b.rsp_instruction_id
    ) 

请帮忙。感谢。

1 个答案:

答案 0 :(得分:0)

以下是针对我能找到的最明显问题的解决方案。如果您提供表格结构和一些样本,则可以对此进行测试。

UPDATE IAM_RSP_INSTRUCTION j 
   SET j.TOTAL_DURATION = (select (paid_rsp + rd)  as total 
                             from (select count (b.rsp_instruction_id) as paid_rsp, 
                                          a.rsp_duration as rd,
                                          b.rsp_instruction_id 
                                     from iam_rsp_instruction a 
                                          JOIN 
                                          iam_buy_fund_info b 
                                          on a.RSP_ID = b.RSP_INSTRUCTION_ID
                                    where b.RSP_INSTRUCTION_ID is not null 
                                      and (a.rsp_status= 'approved' 
                                       or a.rsp_status='terminated')
                                      and b.rsp_instruction_id = j.rsp_id
                                 group by a.rsp_duration,
                                          b.rsp_instruction_id, 
                                          a.rsp_status 
                                   HAVING a.rsp_duration > -1));