Two tables are given as shown picture below. First columns are unique. You should join and generate target table as shown picture below. Target table must contains all data. But there is a exception. If second column (col_2) of the first table (tbl_1) is “-1”, surpluss(excess) data of second table (tbl_2) will match col_2= “-1” row. In other words, surpluss(excess) data of tbl_2, will be match “-1”. If there is no “-1” then new row will be add. All data of tbl_1 and tbl2 must be included in target table. note: col_2 of tbl_2 can not be “-1”. But col_2 of tbl_1 has some “-1”. The scenario looks like foolish but I come across it at work 🙂
Aşağıdaki resimde gözüken 2 tablo verilmiştir. Bu tabloların ilk iki sütunu unique’dir. Bu iki tabloyu joinleyip, aşağıdaki target tabloyu oluşturmanız beklenmektedir. Target tabloda tbl_1 ve tbl_2 den gelen tüm data olmak zorundadır. Fakat şöyle bir istisna var. İkinci tablodaki (tbl_2) fazla datayı, birinci tablo ( tbl_1) ikinci sütunda ( tbl_2) “-1” olanın olduğu satıra yazılacak. Eğer tbl_1 de -1 li kayıt yoksa yeni satır olarak gelecek. Not: tbl_1 deki col_2 sütunu -1 değer alabilir fakat tbl_2’deki col_2 sütunu -1 değerini alamaz. Senaryo saçma gibi gözükebilir ama geçenlerde iş yerinde böyle bir durumla karşılaştım 🙂
CREATE TABLE tbl_1 (
col_1 int,
col_2 int,
data_t1 varchar2(1),
CONSTRAINT cnst_uq UNIQUE(col_1,col_2)
);
CREATE TABLE tbl_2 (
col_1 int,
col_2 int CONSTRAINT cnst_chk CHECK (col_2 <>-1),
data_t2 varchar2(1),
CONSTRAINT cnst_uq2 UNIQUE(col_1,col_2)
);
INSERT INTO tbl_1 VALUES (1,100,'x');
INSERT INTO tbl_1 VALUES (2,101,'y');
INSERT INTO tbl_1 VALUES (2,102,'z');
INSERT INTO tbl_1 VALUES (3,100,'t');
INSERT INTO tbl_1 VALUES (4,100,'x');
INSERT INTO tbl_1 VALUES (5,103,'y');
INSERT INTO tbl_1 VALUES (5,104,'z');
INSERT INTO tbl_1 VALUES (5,105,'t');
INSERT INTO tbl_1 VALUES (6,-1,'x');
INSERT INTO tbl_1 VALUES (7,101,'y');
INSERT INTO tbl_1 VALUES (7,-1,'z');
INSERT INTO tbl_1 VALUES (8,-1,'x');
COMMIT;
INSERT INTO tbl_2 VALUES (1,100,'a');
INSERT INTO tbl_2 VALUES (2,101,'s');
INSERT INTO tbl_2 VALUES (2,102,'d');
INSERT INTO tbl_2 VALUES (3,100,'f');
INSERT INTO tbl_2 VALUES (3,102,'a');
INSERT INTO tbl_2 VALUES (5,103,'s');
INSERT INTO tbl_2 VALUES (5,107,'d');
INSERT INTO tbl_2 VALUES (5,108,'f');
INSERT INTO tbl_2 VALUES (6,101,'a');
INSERT INTO tbl_2 VALUES (6,102,'s');
INSERT INTO tbl_2 VALUES (7,101,'d');
INSERT INTO tbl_2 VALUES (7,102,'f');
INSERT INTO tbl_2 VALUES (7,104,'a');
COMMIT;