将表中的每条记录和计算的记录循环到Oracle中的其他表中

时间:2010-10-23 02:24:26

标签: sql oracle plsql oracle10g

我是Oracle 10g的新手。我有几个表,如下所示:

INCOME_MASTER
INCOME_ID     NUMBER(10) sEQ NUMBER,
INCOME_TYPE   VARCHAR2(10),
INCOME_DATE   DATE

INCOME_DETAILS
INCOME_DETAILS_SEQ_NO     NUMBER(10) SEQUENCE,
INCOME_ID                 NUMBER(10),
ITEM_ID                   NUMBER(10),
ITEM_VALUE                NUMBER (10,2)

我在两个表中都有很多值。

我需要查询INCOME_MASTER表中的所有记录,并且对于INCOME_MASTER表中的每条记录,我需要循环遍历

SELECT INCOME_ID FROM INCOME_MASTER
select ITEM_VALUE FROM INCOME_DETAILS WHERE INCOME_ID= FROM above query and ITEM_ID=201
select ITEM_VALUE FROM INCOME_DETAILS WHERE INCOME_ID= FROM above query and ITEM_ID=202
select ITEM_VALUE FROM INCOME_DETAILS WHERE INCOME_ID= FROM above query and ITEM_ID=203
select ITEM_VALUE FROM INCOME_DETAILS WHERE INCOME_ID= FROM above query and ITEM_ID=204

我需要从ITEM_ID 202,203,204添加值并从ITEM_ID = 201值中扣除该值

无论我得到什么价值,我都需要在INCOME_DETAILS

中插入新记录
insert into INCOME_DETAILS 
  values(INCOME_ID,205,value from above)

我想对Income_master表中的所有记录执行此操作

2 个答案:

答案 0 :(得分:0)

我的神谕是生锈的,但它应该是这样的...... 在处理sql时,你想要在集合中而不是逐行思考。 您可以对第一行进行注释并运行它以预览将插入到表中的数据。

INSERT INTO INCOME_DETAILS(INCOME_ID, ITEM_ID, ITEM_VALUE)
SELECT  INCOME_MASTER.INCOME_ID, 205, b+c+d-a
FROM    INCOME_MASTER
        INNER JOIN 
        (
            SELECT  INCOME_MASTER.INCOME_ID, 
                    SUM(DECODE(ITEM_ID, 201, ITEM_VALUE, 0)) a,
                    SUM(DECODE(ITEM_ID, 202, ITEM_VALUE, 0)) b,
                    SUM(DECODE(ITEM_ID, 203, ITEM_VALUE, 0)) c,
                    SUM(DECODE(ITEM_ID, 204, ITEM_VALUE, 0)) d
            FROM    INCOME_MASTER
                    INNER JOIN INCOME_DETAILS
                        ON INCOME_MASTER.INCOME_ID = INCOME_DETAILS.INCOME_ID
            WHERE   INCOME_DETAILS.ITEM_ID IN (201,202,203,204)
            GROUP BY INCOME_MASTER.INCOME_ID
        ) details
            ON INCOME_MASTER.INCOME_ID = details.INCOME_ID;

答案 1 :(得分:0)

我同意clyc,你需要集思考。除非我们缺少详细信息,否则此操作不需要任何过程代码。

示例数据:

create table income_master (
   income_id   number(10),
   income_type varchar2(10),
   income_date date
   );

create table income_details (
   income_details_seq_no number(10),
   income_id             number(10),
   item_id               number(10),
   item_value            number(10,2)
   );

insert into income_master values (1,'a',sysdate);
insert into income_master values (2,'b',sysdate);
insert into income_master values (3,'c',sysdate);
insert into income_master values (4,'d',sysdate);

insert into income_details values(1,1,201,1);
insert into income_details values(2,1,202,20);
insert into income_details values(3,1,203,300);
insert into income_details values(4,1,204,4000);

insert into income_details values(5,2,201,1);
insert into income_details values(6,2,202,20);
insert into income_details values(7,2,203,300);

insert into income_details values(8,3,201,10);
insert into income_details values(9,3,202,20);
insert into income_details values(10,3,203,30);

insert into income_details values(11,4,202,-90);
insert into income_details values(12,4,202,-10);

插入声明:

INSERT INTO Income_Details (Income_Id, Item_Id, Item_Value) 
(
  SELECT ma.Income_Id, 205, SUM(DECODE(de.Item_ID,201,-1,1) * de.Item_Value) 
  FROM Income_Master ma
  JOIN Income_Details de ON ma.Income_Id = de.Income_Id 
     AND de.Item_Id BETWEEN 201 and 204
  GROUP BY ma.Income_Id, 205
);

结果:

select Income_Id, Item_Id, Item_Value from income_details WHERE Item_Id = 205;
INCOME_ID              ITEM_ID                ITEM_VALUE             
---------------------- ---------------------- ---------------------- 
3                      205                    40                     
1                      205                    4319                   
2                      205                    319                    
4                      205                    -100