简体   繁体   English

如何仅从 Oracle SQL 中包含特定字符串的列中获取 select 和别名数据

[英]How to select and alias data only from the column that contains a certain string in Oracle SQL

I am working with address records in an Oracle db.我正在处理 Oracle 数据库中的地址记录。 Each row contains information on two parents.每行包含两个父母的信息。 There are four columns for phone number types and four columns for numbers.电话号码类型有四列,数字有四列。 The types are Other_No_Type_1, Other_No_Type_2, Other_No_Type_3, Other_No_Type_4 and any one of them might contain a value of either 'Name1:Mobile', 'Name2:Mobile', 'Father Work', or 'Mother Work', That value refers to the number in the next column (Other_No_1, Other_No_2, Other_No_3 or Other_No_4).这些类型是 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)中。 I need to pull the Other_No_x value when Other_No_Type_x is equal to Name1:Mobile and alias it "contact_1_mobile" and pull Name2:Mobile and alias it "contact_2_mobile".当 Other_No_Type_x 等于 Name1:Mobile 时,我需要提取 Other_No_x 值并将其别名为“contact_1_mobile”,然后提取 Name2:Mobile 并将其别名为“contact_2_mobile”。 In my SELECT below, you can see that I've just written "a.other_no_1 as contact_1_mobile" for example, but actually that might be retrieving a work number or the Name2:mobile number.在我下面的 SELECT 中,您可以看到我刚刚写了例如“a.other_no_1 as contact_1_mobile”,但实际上这可能是检索工作号码或 Name2:mobile 号码。 This is my first request for help to the forum, so I apologize for probably not presenting my question properly.这是我第一次向论坛寻求帮助,所以对于可能没有正确提出我的问题,我深表歉意。 Thank you for any help you can give.感谢您提供的任何帮助。 Here is my statement as it stands now:这是我现在的声明:

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

Just use case or decode:只需用例或解码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM