2016-09-02 2 views
0

oracle에서 내부 뷰를 사용하여 여러 행을 업데이트하려고합니다.Oracle에서 다른 값으로 여러 행을 업데이트하십시오.

이보기를 업데이트하는 SELECT 문은 다음과 같습니다 내가 노력하고

select count(distinct a.numCount) as numCount, a.accNum as accNum , 
s.unitNum as unitNum 
from tableA a,tableS s where a.accNum is not null and s.fk_id= 
(select id from tableD where sid=a.accNum) 
group by a.accNum ,s.unitNum ; 

업데이트 문은 다음과 같습니다 :

update 
(select count(distinct a.numCount) as numCount, a.accNum as accNum , 
s.unitNum as unitNum 
from tableA a,tableS s where a.accNum is not null and s.fk_id= 
(select id from tableD where sid=a.accNum) 
group by a.accNum ,s.unitNum) k 
set k.unitNum=k.numCount; 

내가 numCount의 값으로 unitNum을 업데이트하려합니다. 위 쿼리는보기로 사용하면 작동하지 않습니다. 오라클에서이를 업데이트하는 또 다른 방법이 있습니까?

좋습니다. 테이블의

구조는 다음과 같습니다 :

TableA 

accNum numCount 
----------------------- 
111  1 
222  5 
333  2 
111  1 
111  1 
222  5 
222  2 


TableS 

fk_id unitNum 
----------------------- 
123  0 
768  0 
734  0 

TableD 

ID  sid 
----------------------- 
123  222 
768  111 
734  333 

출력은 다음과 같아야합니다

TableS 

fk_id unitNum 
----------------------- 
123  3 
768  3 
734  1 

어떻게 위의 쿼리가 업데이트됩니다

+0

제안하세요? –

+0

테이블 구조에 대해 더 자세히 설명해 주시겠습니까? –

+1

'update' 문 –

답변

0
update tableS s 
    set unitNum= 
     (select count(distinct a.numCount) as numCount 
      from tableA a, tableD d 
      where s.fk_id=d.id and d.sid=a.accNum 
     );