在LISTAGG选项上需要一些帮助

时间:2019-01-14 21:11:11

标签: oracle listagg

LISTAGG的字符数限制为4000。谁能帮这个噩梦般的查询?

您好,我在提供应用程序的查询中有此错误代码。在这种形式下,查询没有实际条件。因此,LISTAGG达到了4000个字符的限制。

这里还有其他选择吗?根据所有内容的连接方式,我看不到一个简单的答案:

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;

1 个答案:

答案 0 :(得分:1)

LISTAGG的4000个字符限制的经典解决方法是使用XMLAGG,如this famous AskTom post中所述。

语法有点复杂。在您的用例中,您需要替换此:

    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

使用:

    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