【问题标题】:How to select and alias data only from the column that contains a certain string in Oracle SQL如何仅从包含 Oracle SQL 中特定字符串的列中选择和别名数据
【发布时间】: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

【问题讨论】:

  • 建议:通过分段或分点使解释更具可读性
  • 这是我在这个网站上的第一个问题。谢谢你的建议,我以后会这样做的。

标签: sql oracle


【解决方案1】:

只是用例或解码:

case 'Name1:Mobile' 
when Other_No_Type_1 then Other_No_1
when Other_No_Type_2 then Other_No_2
when Other_No_Type_3 then Other_No_3
when Other_No_Type_4 then Other_No_4
end as contact_1_mobile

【讨论】:

  • 谢谢你,萨扬。我知道我错过了一些简单的东西和我以前做过的东西,但是我不能,不能,不能想出它!
猜你喜欢
  • 1970-01-01
  • 2017-09-24
  • 2019-10-22
  • 2013-06-27
  • 1970-01-01
  • 2021-12-13
  • 1970-01-01
  • 2018-10-25
  • 1970-01-01
相关资源
最近更新 更多