2016-03-21 89 views
0

我想從表IAM_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 

目前更新TOTAL_DURATION列中的值數據,我有一個更新查詢:

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 
    ) 

請幫忙。謝謝。

+2

您正在使用哪些DBMS? sql srver <> oracle。你需要定義「不工作」。 –

+0

請發佈錯誤信息。 –

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