JOIN Tables based on the column value in oracle

Viewed 79

I'm trying to join a table based on the column values like (A&B)||(C&D). I can create a join for A&B and C&D separately. But how will I accomplish the OR condition in between?

[UPDATE] A, B, C and D are the level_ids in dummy_level table. So the ground rule for joining the table is (21 && 22) || (23 && 24)

My table structure is,


CREATE TABLE dummy_cust(
  id INT,
  NAME VARCHAR(50),
  age INT,
  country VARCHAR(50));

INSERT INTO dummy_cust VALUES (1,'KIM',23,'US');
INSERT INTO dummy_cust VALUES (2,'KOM',24,'US');
INSERT INTO dummy_cust VALUES (3,'KING',29,'US');
INSERT INTO dummy_cust VALUES (4,'RAM',29,'US');
INSERT INTO dummy_cust VALUES (5,'RAV',23,'US');
INSERT INTO dummy_cust VALUES (6,'KIV',25,'US');

CREATE TABLE dummy_level(
  id INT,
  level_id INT,
  STATUS VARCHAR(50));

INSERT INTO dummy_level VALUES (1,21,'C');
INSERT INTO dummy_level VALUES (1,22,'C');
INSERT INTO dummy_level VALUES (1,24,'C');
INSERT INTO dummy_level VALUES (2,22,'C');
INSERT INTO dummy_level VALUES (2,23,'C');
INSERT INTO dummy_level VALUES (2,24,'C');
INSERT INTO dummy_level VALUES (3,21,'Z');
INSERT INTO dummy_level VALUES (3,22,'Z');
INSERT INTO dummy_level VALUES (4,21,'Z');
INSERT INTO dummy_level VALUES (4,22,'Z');
INSERT INTO dummy_level VALUES (4,24,'Z');

Expected Result:


+ --- + ---- + --- + ------- + -------- + ------- + -------- + ------- + -------- + ------- + -------- + ------- +
| id  | name | age | country | Level ID | Status  | Level ID | Status  | Level ID | Status  | Level ID | Status  |
+ --- + ---- + --- + ------- + -------- + ------- + -------- + ------- + -------- + ------- + -------- + ------- +
| 1   | KIM  | 23  | US      | 21       | C       | 22       | C       |          |         | 24       | C       |
| 2   | KOM  | 24  | US      |          |         | 22       | C       | 23       | C       | 24       | C       |
| 3   | KING | 29  | US      | 21       | Z       | 22       | Z       |          |         |          |         |
| 4   | RAM  | 29  | US      | 21       | Z       | 22       | Z       |          |         | 24       | Z       |
+ --- + ---- + --- + ------- + -------- + ------- + -------- + ------- + -------- + ------- + -------- + ------- +


Please help to solve this. Slightly handicapped as I'm new to this. I badly need the experts to help here in solving this.

1 Answers

You are pivoting some values, so easiest is to join and use pivot:

select *
  from dummy_cust c 
  join dummy_level  l using (id)
  pivot (max(level_id) lvl, max(status) sts 
         for level_id in (21 as l21, 22 as l22, 23 as l23, 24 as l24) )
  order by id         

dbfiddle demo

If I understand your comment correctly, you want to show rows when they have both levels 21 and 22 or both levels 23 and 24. So:

select *
  from (
    select c.id, c.name, c.age, c.country, l.level_id, l.status,
           count(case level_id when 21 then 1 end) over (partition by c.id) c21,
           count(case level_id when 22 then 1 end) over (partition by c.id) c22,
           count(case level_id when 23 then 1 end) over (partition by c.id) c23,
           count(case level_id when 24 then 1 end) over (partition by c.id) c24
      from dummy_cust c 
      join dummy_level l on c.id = l.id )
  where (c21 > 0 and c22 > 0) or (c23 > 0 and c24 > 0)

Then you can make pivot for presentation.

Related