Search notes:

Oracle: Partitioned outer join

A paritioned outer join fills gaps in sparse data.
drop table tq84_article;
create table tq84_item (
   id         integer primary key,
   name       varchar2(20) not null
);
 
create table tq84_sale (
   dt         date,
   id_item    not null references tq84_item,
   qty        number(3) not null
);
 
 
 
-- dates for locations are "gappy":
-- none of the locations has entries for all 3 dates
-- ( date range: 2019-01-15 - 2019-01-17 )
begin
   insert into tq84_item values (1, 'ABC');
   insert into tq84_item values (2, 'PQR');
   insert into tq84_item values (3, 'XYZ');
  
   insert into tq84_sale values (date '2024-07-01', 1,  13);
   insert into tq84_sale values (date '2024-07-01', 2, 697);
   insert into tq84_sale values (date '2024-07-01', 3,   6);
  
   insert into tq84_sale values (date '2024-07-02', 1,  10);
   insert into tq84_sale values (date '2024-07-02', 2, 612);
-- insert into tq84_sale values (date '2024-07-02', 3,    );  -- NO XYZ sold on 2024-07-02
 
-- insert into tq84_sale values (date '2024-07-03', 1,    );  -- NO ABC sold on 2024-07-03
   insert into tq84_sale values (date '2024-07-03', 2, 572);
   insert into tq84_sale values (date '2024-07-03', 3,    2);
 
   commit;
end;
/
 
 
select
   to_char(sal.dt, 'yyyy-mm-dd') day_,
   itm.name,
   sal.qty
from
   tq84_sale  sal  partition by (dt)
                   right join
   tq84_item  itm  on sal.id_item = itm.id
order by
   sal.dt,
   itm.name;
--
-- DAY_                                                                        NAME                        QTY
-- --------------------------------------------------------------------------- -------------------- ----------
-- 2024-07-01                                                                  ABC                          13
-- 2024-07-01                                                                  PQR                         697
-- 2024-07-01                                                                  XYZ                           6
-- 2024-07-02                                                                  ABC                          10
-- 2024-07-02                                                                  PQR                         612
-- 2024-07-02                                                                  XYZ                           
-- 2024-07-03                                                                  ABC                           
-- 2024-07-03                                                                  PQR                         572
-- 2024-07-03                                                                  XYZ                           2  

Execution plan

select * from dbms_xplan.display(format=>'basic');
--------------------------------------------------
| Id  | Operation                    | Name      |
--------------------------------------------------
|   0 | SELECT STATEMENT             |           |
|   1 |  SORT ORDER BY               |           |
|   2 |   VIEW                       |           |
|   3 |    MERGE JOIN PARTITION OUTER|           |
|   4 |     SORT JOIN                |           |
|   5 |      TABLE ACCESS FULL       | TQ84_ITEM |
|   6 |     SORT PARTITION JOIN      |           |
|   7 |      TABLE ACCESS FULL       | TQ84_SALE |
--------------------------------------------------

Links

This example is inspired and mostly copied from this dba.stackexchange.com answer.

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1758199275, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/databases/Oracle/SQL/join/outer/partitioned(116): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78