[英]How to select and alias data only from the column that contains a certain string in Oracle SQL
我正在處理 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 時,我需要提取 Other_No_x 值並將其別名為“contact_1_mobile”,然后提取 Name2:Mobile 並將其別名為“contact_2_mobile”。 在我下面的 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
只需用例或解碼:
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.