简体   繁体   中英

Need some assistance with LISTAGG options

LISTAGG has a 4000 character limitation. Can anyone help with this nightmarish query?

Hi, I have this bad code in a query which feeds an application. In this form, the query has no real criteria. And because of that, LISTAGG is hitting the 4000 character limit.

Is there any other option here? Based on how everything is concatenated, I am not seeing a simple answer:

SELECT 
    email,
    first_name,
    last_name, 
    LISTAGG(
        type_id 
        ||','|| type_status 
        ||','|| filename 
        ||','|| status_id
        ||','|| status_message 
        ||','|| TO_CHAR(upload_date,'MM/DDYYYY') 
        ||','|| broker 
        ||','|| quarter 
        ||','|| quarter_year
        ||':'
    ) WITHIN GROUP (ORDER BY upload_date DESC) filenames
FROM 
    (
        SELECT 
            b.surrogate_key, 
            b.email,
            b.type_id, 
            DECODE(a.file_type,NULL,0,1) type_status, 
            a.quarter, 
            a.quarter_year, 
            a.broker, 
            a.upload_date, 
            a.filename, 
            a.status_id, 
            b.first_name, 
            b.last_name, 
            a.status_message
        FROM 
            (
                SELECT DISTINCT
                    (editor || '~' || file_type) surrogate_key,
                    file_type, 
                    quarter, 
                    quarter_year, 
                    broker,
                    filename,
                    upload_date,
                    status_id, 
                    status_message
                FROM upload_history
                ORDER BY upload_date DESC
            ) a, 
            (
                SELECT DISTINCT
                    (email||'~'||type_id) surrogate_key, 
                    email, 
                    type_id, 
                    first_name, 
                    last_name
                FROM issuers
                CROSS JOIN file_types 
            ) b
        WHERE 
            b.surrogate_key = a.surrogate_key(+)
    ) 
GROUP BY 
    email,
    first_name, 
    last_name, 
    type_id, 
    quarter, 
    quarter_year;

The classical workaround the 4000 chars limitation of LISTAGG is to use XMLAGG , as explained in this famous AskTom post .

The syntax is a bit convoluted. In your use case, you would need to replace this :

    LISTAGG(
        type_id 
        ||','|| type_status 
        ||','|| filename 
        ||','|| status_id
        ||','|| status_message 
        ||','|| TO_CHAR(upload_date,'MM/DDYYYY') 
        ||','|| broker 
        ||','|| quarter 
        ||','|| quarter_year
        ||':'
    ) WITHIN GROUP (ORDER BY upload_date DESC) filenames

With :

    REPLACE(REPLACE(
        XMLAGG(XMLELEMENT("a", 
            type_id 
            ||','|| type_status 
            ||','|| filename 
            ||','|| status_id
            ||','|| status_message 
            ||','|| TO_CHAR(upload_date,'MM/DDYYYY') 
            ||','|| broker 
            ||','|| quarter 
            ||','|| quarter_year
            ||':'
        ) ORDER BY upload_date DESC).getClobVal(),
    '<a>', ''), '</a>', ',') filenames

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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