Table T_1 (index_1, crop_id, corp_name, start_time, end_time ),
T_2 (index_2, corp_id, corp_name, year_time, month_time) needs to be generated ).
Index_1 and index2 are generated by sequence.
The data is as follows:
T_1
Index_1, corp_id, corp_name, start_time, end_time
101 1 Microsoft 2007-5 2007-7
102 2 IBM 2008-1 2008-2
T_2
Index_2, corp_id corp_name year_time month_time
8 1 Microsoft 2007 5
9 1 Microsoft 2007 6
10 1 Microsoft 2007 7
112 IBM 2008 1
122 IBM 2008 2
Http://topic.csdn.net/u/20080719/09/42593213-94fb-40ca-b4dc-5991e2a5615d.html
Answer:
Write one with a stored procedure without considering the cross-year comparison of a single corp_name!
-
SQL code
-
-- Create t_1create table T_1 (corp_id int, corp_name varchar (10), start_time date, end_time date) -- create t_2create table T_2 (corp_id int, corp_name varchar (10), year_time date, month_time date) -- insert into T_1 values ('1', 'Microsoft ', to_date ('2017-05', 'yyyy-mm '), to_date ('1970-07 ', 'yyyy-mm') insert into T_1 values ('2', 'ibm', to_date ('1970-01 ', 'yyyy-mm'), to_date ('1970-02 ', 'yyyy-mm') -- create or replace procedure pro_test_a is I int; j int; a01 int; A02 varchar2 (10); A03 date; a04 date; a05 int; a06 int; tmpa int; tmpb int; cursor cur_q is select * From T_1; begin open cur_q; loop fetch cur_q into A01, A02, A03, a04; exit when cur_q % notfound; tmpa: = month (A03); tmpb: = month (a04); a06: = year (A03); a05: = tmpb-tmpa; J: = tmpA-1; for I in 0 .. a05 loop J: = J + 1; insert into T_2 values (A01, A02, a06, J); Commit; end loop; close cur_q; end; ______________ SQL: exec pro_test_a ______________________ result: 1 2 IBM 2008 12 2 IBM 2008 23 1 Microsoft 2007 54 1 Microsoft 2007 65 1 Microsoft 2007 7