【发布时间】:2020-12-08 05:33:04
【问题描述】:
我正在处理 Oracle 数据库中的地址记录。每行包含有关两个父母的信息。电话号码类型有四列,数字有四列。类型为 Other_No_Type_1、Other_No_Type_2、Other_No_Type_3、Other_No_Type_4,其中任何一个都可能包含“Name1:Mobile”、“Name2:Mobile”、“Father Work”或“Mother Work”的值,该值指的是数字在下一列(Other_No_1、Other_No_2、Other_No_3 或 Other_No_4)。当 Other_No_Type_x 等于 Name1:Mobile 并将其别名为“contact_1_mobile”并提取 Name2:Mobile 并将其别名为“contact_2_mobile”时,我需要提取 Other_No_x 值。在下面的 SELECT 中,您可以看到我刚刚写了“a.other_no_1 as contact_1_mobile”,但实际上这可能是检索工作号码或 Name2:mobile 号码。这是我第一次向论坛寻求帮助,所以我很抱歉可能没有正确地提出我的问题。感谢您提供的任何帮助。这是我现在的声明:
SELECT final.*
FROM (
SELECT
--Name 1 in P1 household
a.id
,a.name1_web_user_id as contact_1_id
,a.name1_full_name as contact_1_name
,a.other_no_1 as contact_1_mobile (THIS IS MY PROBLEM. "Other_No_1" MAY NOT ACTUALLY BE THE NAME1:MOBILE NUMBER TYPE I NEED. THIS PROBLEM IS THE SAME IN EACH SECTION OF MY STATEMENT.)
,a.email as contact_1_email
--Name 2 in P1 household
,a.name2_web_user_id as contact_2_id
,a.name2_full_name as contact_2_name
,a.other_no_2 as contact_2_mobile (PROBLEM: HERE I ACTUALLY NEED TO FIND THE COLUMN THAT CONTAINS THE "Name2:Mobile" NUMBER)
,a.EMAIL_2 as contact_2_email
FROM rg_student s left outer join rg_addr a on s.id = a.id
WHERE (
(a.addr_code='P1' AND a.rg_active = 'Y') AND ((a.name1_web_user_id is not null) OR (a.name2_web_user_id is not null))
AND a.id in(SELECT id from rg_student where student_group='Student')
)
UNION
SELECT
--Name 1 in P2 household
a.id
,a.name1_web_user_id as contact_3_id
,a.name1_full_name as contact_3_name
,a.other_no_1 as contact_3_mobile (PROBLEM LINE)
,a.email as contact_3_email
--Name 2 in P2 household
,a.name2_web_user_id as contact_4_id
,a.name2_full_name as contact_4_name
,a.other_no_2 as contact_4_mobile (PROBLEM LINE)
,a.EMAIL_2 as contact_4_email
FROM rg_student s left outer join rg_addr a on s.id = a.id
WHERE (
(a.addr_code='P2' AND a.rg_active = 'Y') AND ((a.name1_web_user_id is not null) OR (a.name2_web_user_id is not null))
AND a.id in(SELECT id from rg_student where student_group='Student')
)
)final
ORDER BY final.id
【问题讨论】:
-
建议:通过分段或分点使解释更具可读性
-
这是我在这个网站上的第一个问题。谢谢你的建议,我以后会这样做的。