Hi Tom,
there is a table with all libraries and books.
there is other table with book shells. the book can be included or explicit excluded to/from shell.
I have to populate lib_shell table with connections between libraries and shells
create table lib_book (
lib_name VARCHAR2(7 CHAR) not null,
book_name VARCHAR2(7 CHAR) not null
);
create table shell_book (
shell_id number not null,
book_name VARCHAR2(7 CHAR) not null,
is_excluded number not null -- 0 - included, 1 - excluded
);
insert into lib_book values ('lib1', 'book1');
insert into lib_book values ('lib1', 'book2');
insert into lib_book values ('lib2', 'book1');
insert into lib_book values ('lib2', 'book2');
insert into lib_book values ('lib2', 'book3');
--- ca 300 000 000 rows (6 000 000 libraries with 1-100 books on each)
insert into shell_book values (1, 'book2', 0);
insert into shell_book values (2, 'book1', 0);
insert into shell_book values (2, 'book2', 0);
insert into shell_book values (2, 'book4', 1);
insert into shell_book values (3, 'book1', 0);
insert into shell_book values (3, 'book3', 1);
-- ca 60 000 rows (1000 shells with 1-60 books on each)
create table lib_shell (
lib_name VARCHAR2(7 CHAR) not null,
shell_id number not null
)
-- pupulate lib_shell here
-- results in this case:
--'lib1',1
--'lib1',2
--'lib1',3
--'lib2',1
--'lib2',2
thanks a lot
Sounds to me like you just need to join, filtering out the rows where is_excluded = 1.
You can then insert the query into the table:
create table lib_book (
lib_name VARCHAR2(7 CHAR) not null,
book_name VARCHAR2(7 CHAR) not null
);
create table shell_book (
shell_id number not null,
book_name VARCHAR2(7 CHAR) not null,
is_excluded number not null -- 0 - included, 1 - excluded
);
insert into lib_book values ('lib1', 'book1');
insert into lib_book values ('lib1', 'book2');
insert into lib_book values ('lib2', 'book1');
insert into lib_book values ('lib2', 'book2');
insert into lib_book values ('lib2', 'book3');
insert into shell_book values (1, 'book2', 0);
insert into shell_book values (2, 'book1', 0);
insert into shell_book values (2, 'book2', 0);
insert into shell_book values (2, 'book4', 1);
insert into shell_book values (3, 'book1', 0);
insert into shell_book values (3, 'book3', 1);
create table lib_shell (
lib_name VARCHAR2(7 CHAR) not null,
shell_id number not null
);
insert into lib_shell
select lib_name, shell_id
from lib_book l
join shell_book s
on s.book_name = l.book_name
where is_excluded = 0;
select * from lib_shell;
LIB_NAME SHELL_ID
lib1 3
lib1 2
lib1 2
lib1 1
lib2 3
lib2 2
lib2 2
lib2 1
If you want to exclude duplicates, use distinct after the select:
select distinct lib_name, shell_id