Oracle Replace function

前端 未结 3 1676
死守一世寂寞
死守一世寂寞 2021-01-23 17:09

I need to replace the Table1\'s filed values from Table2\'s values while select query.

Eg:

Table1:

Org                  Permission
--------------         


        
3条回答
  •  孤街浪徒
    2021-01-23 17:24

    If you don't want to update the existing table and only want to select the data then you can use this somewhat laborious query.

    http://sqlfiddle.com/#!4/22909/4

    WITH changed_table AS
         (SELECT val1, EXTRACTVALUE (x.COLUMN_VALUE, 'e') val2new
            FROM (SELECT val1, val2 xml_str
                    FROM table1),
                 TABLE (XMLSEQUENCE (XMLTYPE (   ''
                                              || REPLACE (xml_str, ',', '')
                                              || ''
                                             ).EXTRACT ('e/e')
                                    )
                       ) x)
    SELECT ct.val1, listagg(table2.val2,',') within group (order by table2.val2) val2
      FROM changed_table ct, table2 table2
     WHERE ct.val2new = table2.val1
    group by ct.val1;
    

    I have used the XMLTYPE to separate the comma separated numbers to rows. Then joined the rows with second table to get the description and finally used the LISTAGG function to form comma separated string. Don't know how efficient this query is. I agree with Mark Bannister's comment.

提交回复
热议问题