Fill Missing Oracle Data

Believe me, I tried to find luck for this problem. I had MYSQL and SQLServer solutions, not Oracle, and not the specific convolution I need. Since Cross Apply is not available in the version of Oracle I'm using, I hit the road block.

The problem is simple for many of you.

The beginning of the universe for me is 13 months.

I have a table with

CREATE TABLE TBLTESTAUM (
ORDER_NO NUMBER(10,0) NOT NULL ENABLE,
RECORD_DATE DATE,
Order_SEQUENCE NUMBER(5,0) NOT NULL ENABLE,
CLASS NUMBER(3,0));

INSERT INTO TBLTESTAUM VALUES (1234, '29-Aug-2015', 34, 459);
INSERT INTO TBLTESTAUM VALUES (1234, '20-Jun-2016', 35, 877);
INSERT INTO TBLTESTAUM VALUES (1234, '20-Jun-2016', 37, 877);
INSERT INTO TBLTESTAUM VALUES (1234, '02-Jul-2016', 39, 122);
INSERT INTO TBLTESTAUM VALUES (1234, '28-Jul-2016', 40, 122);
INSERT INTO TBLTESTAUM VALUES (1234, '31-Jul-2016', 41, 311);
INSERT INTO TBLTESTAUM VALUES (1234, '10-Aug-2016', 42, 311);
INSERT INTO TBLTESTAUM VALUES (1234, '18-Aug-2016', 44, 110);
INSERT INTO TBLTESTAUM VALUES (1234, '20-Aug-2016', 45, 110);

Note:

  • 20 / Jul / 2015 in the first box.
  • The Seq field may or may not have each value. Perhaps some of them are missing. If you want to use it.
  • Not Nullable for Sequence is ignored.

So, 13 months give me 07/22/2015, today.

, "CLASS", , CLASS. , , . CLASS.

-

Order Num   WeekDate    CLASS
123         27-Jul-15   459
123         3-Aug-15    459
123         10-Aug-15   459
123         17-Aug-15   459
123         24-Aug-15   459
123         31-Aug-15   459
123         7-Sep-15    459
Dates and Order Num to continue till next match in TBLTESTAUM is found
123         20-Jun-16   877
123         27-Jun-16   122
123         4-Jul-16    122
123         11-Jul-16   122
123         25-Jul-16   311
123         1-Aug-16    311
123         8-Aug-16    311
123         15-Aug-16   110
123         22-Aug-16   110

. , .

.
. , , .

.

PS: 10 . . , .

+4
1

, . , , , . (, , , ).

(, , , " ", ). order_no , , , ; "" order_no, , CTE "o" , tbltestaum.

!

with
     w ( weekdate ) as (
       select trunc(sysdate, 'iw') - 7 * (level - 1) + 6  -- This will generate Sundays
       from   dual
       connect by level <= 1 + 
                   ( trunc(sysdate, 'iw') - trunc(add_months(sysdate, -13), 'iw') ) / 7
     ), 
     o ( order_no ) as (
       select distinct order_no from tbltestaum
     ),
     prep ( order_no, dt, order_sequence, class ) as (
       select order_no, record_date, order_sequence, class
         from tbltestaum
       union all
       select order_no, weekdate, null, null
         from w cross join o
     ),
     z ( order_no, dt, order_sequence, class ) as (
       select order_no, dt, order_sequence,
              nvl( last_value(class ignore nulls) over (partition by order_no 
                                                        order by dt, order_sequence),
                  first_value(class ignore nulls) over (partition by order_no
                                                        order by dt, order_sequence
                             rows between unbounded preceding and unbounded following ) )
       from   prep
     )
select order_no, to_char(dt - 6, 'dd-Mon-yy') as weekdate, class
from   z
where  order_sequence is null
;
+1

Source: https://habr.com/ru/post/1652041/


All Articles