再求一UPDATE语句
Create table jj(
jj_id integer,-- 物理主键
REPORT_M_ID INTEGER,--主表ID
ITEM_ID VARCHAR2(20),--项目
Year varchar2(6),--报表年份
Org_Code varchar2(10),--组织机构编码
JAN integer, --1月
FER INTEGER,--2月
MAR INTEGER,--3月
BUSS_TYPE varchar2(10)--经营业务
);
--JJ表,jj_id是物理主键,ITEM_ID+Year+Org_Code+BUSS_TYPE是业务组合主键。
--相同的ITEM_ID+Year+Org_Code,即使BUSS_TYPE不同,但其REPORT_M_ID(主表ID)是一样的
--测试数据
insert into jj select 1,1,'A001_01','201206','H2222',3,3,3,'AA' FROM DUAL;
insert into jj select 2,1,'A001_02','201206','H2222',4,4,4,'AA' FROM DUAL;
insert into jj select 3,1,'A001_03','201206','H2222',5,5,5,'AA' FROM DUAL;
insert into jj select 4,1,'A001_01','201206','H2222',6,6,6,'BB' FROM DUAL;
insert into jj select 5,1,'A001_02','201206','H2222',7,7,7,'BB' FROM DUAL;
insert into jj select 6,1,'A001_03','201206','H2222',8,8,8,'BB' FROM DUAL;
insert into jj select 7,1,'A001_01','201206','H2222',6,6,6,'CC' FROM DUAL;
insert into jj select 8,1,'A001_02','201206','H2222',7,7,7,'CC' FROM DUAL;
insert into jj select 9,1,'A001_03','201206','H2222',8,8,8,'CC' FROM DUAL;
--求UPDATE语句,修改A001_03的值 ,公式A001_03=A001_01+A001_02,修改后结果如下:
insert into jj select 3,1,'A001_03','201206','H2222',7,7,7,'AA' FROM DUAL;
insert into jj select 6,1,'A001_03','201206','H2222',13,13,13,'BB' FROM DUAL;
insert into jj select 9,1,'A001_03','201206','H2222',13,13,13,'CC' FROM DUAL;
--本人以前用惯了SQL SERVER,对ORACLE的UPDATE,DELETE的方式很不熟悉,望提供一些资料学习
------解决方案--------------------UPDATE JJ A
SET (A.JAN, A.FER, A.MAR) =
(SELECT SUM(B.JAN), SUM(B.FER), SUM(B.MAR)
FROM JJ B
WHERE B.ITEM_ID IN( 'A001_01', 'A001_02')
AND A.YEAR = B.YEAR
AND A.ORG_CODE = B.ORG_CODE
AND A.BUSS_TYPE = B.BUSS_TYPE) WHERE A.ITEM_ID='A001_03';