[英]JOIN Tables based on the column value in oracle
I'm trying to join a table based on the column values like (A&B)||(C&D)
.我正在尝试根据
(A&B)||(C&D)
等列值加入一个表。 I can create a join for A&B and C&D separately.我可以分别为 A&B 和 C&D 创建一个连接。 But how will I accomplish the
OR
condition in between?但是我将如何完成两者之间的
OR
条件?
[UPDATE] A, B, C and D are the level_ids in dummy_level table. [更新] A、B、C 和 D 是 dummy_level 表中的 level_id。 So the ground rule for joining the table is (21 && 22) ||
所以加入表格的基本规则是 (21 && 22) || (23 && 24)
(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.
我非常需要专家在这里帮助解决这个问题。
You are pivoting some values, so easiest is to join and use pivot
:您正在旋转一些值,因此最简单的方法是加入和使用
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
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:如果我正确理解您的评论,您希望在它们同时具有 21 和 22 级或同时具有 23 和 24 级时显示行。所以:
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.然后,您可以制作用于演示的枢轴。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.