Oracle's column-change function

Source: Internet
Author: User

1, the column to row, the following data

ID NAME

---------- ------------------------------

Ten AB

bc

Ten CDs

Hi

ij

Mn

6 Rows selected

2. Call Oracle's function

Sql> Select Id,wmsys.wm_concat (name) name from IDTable

2 Group by ID;

ID NAME

---------- --------------------------------------------------------------------------------

Ten AB,BC,CD

Hi,ij,mn

3, Custom

Create or Replace function getstrbylist (FGF in Varchar2,tab_name on VARCHAR2,HZX in

VARCHAR2,HBWBX in Varchar2,hzxcsz on VARCHAR2) return string

----Feature Description: Oracle multiple lines merged into one line

----Parameter Description:

----FGF: separator, insert separator between grouped data

----Tab_name: The data source table for the query

----HZX: Summary Item field

----HBWBX: Merging text items

----HZXCSZ: Summary Item parameter values

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/database/Oracle/

Is

SQL_STR VARCHAR2 (1024);

Result_val VARCHAR2 (1024);

Begin

Sql_str:= ' SELECT TRANSLATE (LTRIM (text, ' @ '), ' *@ ', ' * ' fgf| | "") Researcherlist from

(SELECT row_number () over (PARTITION by N_sec_code ORDER by N_sec_code,lvl DESC) Rn,n_sec_code,

Text from (SELECT N_sec_code, Level Lvl,sys_connect_by_path (C_researcher_code, "@") text from

(SELECT ' | | hzx| | ' as N_sec_code, ' | | hbwbx| | ' as C_researcher_code,row_number () over (PARTITION by

'|| hzx| | ' Order BY ' | | hzx| | ', ' | | hbwbx| | ') X from ' | | tab_name| | ' WHERE ' | | hzx| | ' ='''|| hzxcsz| | "")

A CONNECT by N_sec_code = PRIOR N_sec_code and x-1 = PRIOR x) The WHERE rn=1 order by

N_sec_code ';

EXECute IMMEDIATE sql_str into Result_val;

return result_val;

End Getstrbylist;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.