Need help to store value from three column

空扰寡人 提交于 2019-12-13 09:15:53

问题


SELECT DISTINCT 
                    clt.id,
                    clt.client_p_email,
                    clt.client_s_email,
                    cus.customer_mail
from  client clt,
         customers cus
where clt.id=cus.id
--My Record is comming bellow 
ID          client_p_email                             client_s_email                                         customer_mail
-----       ----------------------------      ----------------------------                          ---------------------------
703         test01@aol.com                          prod01@gamil.com                                            dev01@yahoo.com.                                    
623         ra.ben@yahoo.com                   ra.ben@yahoo.com                                           ea.bowens@gmail.com                                      
965         eighteenman@aol.com               eighteenman@aol.com                                  eighteenman@aol.com                                        
270         aunkurr1@icloud.com                   amirbhai@icloud.com                                 amirbhai@me.com                                         
719         rah1021@yahoo.com                  rh1021@yahoo.com                                          mars77@vrizon.net     

what i want? i want to display all email in one column. but if you notice 703 id has to store three time and 623 has to store only two time becasue two email are same. and 965 will store one time because all are same . please suggest me how can i create an anonymous block that will store value i want
Please help


回答1:


Say you want to insert data in a table like:

create table allEmailTable  (id number, mail varchar2(100))

Assuming you already have your query that gives that result, you may need:

insert into allEmailTable(id, mail)
with yourQuery(ID, client_p_email, client_s_email, customer_mail) as (
  select 703        , 'test01@aol.com'           ,'prod01@gamil.com'   , 'dev01@yahoo.com' from dual union all                                   
  select 623        , 'ra.ben@yahoo.com'         ,'ra.ben@yahoo.com'   ,  'ea.bowens@gmail.com' from dual union all                                      
  select 965        , 'eighteenman@aol.com'      ,'eighteenman@aol.com',  'eighteenman@aol.com' from dual union all                                        
  select 270        , 'aunkurr1@icloud.com'      ,'amirbhai@icloud.com',  'amirbhai@me.com' from dual union all                                         
  select 719        , 'rah1021@yahoo.com'        ,'rh1021@yahoo.com'   ,  'mars77@vrizon.net' from dual
)
select distinct ID, mail
from (
      select id, client_p_email as mail from yourQuery UNION
      select id, client_s_email         from yourQuery UNION
      select id, customer_mail          from yourQuery 
     )

The result:

SQL> select * from allEmailTable;

        ID MAIL
---------- --------------------
       270 amirbhai@icloud.com
       270 amirbhai@me.com
       270 aunkurr1@icloud.com
       623 ea.bowens@gmail.com
       623 ra.ben@yahoo.com
       703 dev01@yahoo.com
       703 prod01@gamil.com
       703 test01@aol.com
       719 mars77@vrizon.net
       719 rah1021@yahoo.com
       719 rh1021@yahoo.com
       965 eighteenman@aol.com

12 rows selected.

Your query will be:

insert into allEmailTable(id, mail)
with yourQuery(ID, client_p_email, client_s_email, customer_mail) as (
  SELECT DISTINCT 
                    clt.id,
                    clt.client_p_email,
                    clt.client_s_email,
                    cus.customer_mail
  from  client clt,
         customers cus
where clt.id=cus.id
)
select distinct ID, mail
from (
      select id, client_p_email as mail from yourQuery UNION
      select id, client_s_email         from yourQuery UNION
      select id, customer_mail          from yourQuery 
     )


来源:https://stackoverflow.com/questions/41708250/need-help-to-store-value-from-three-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!