-- FunctionCode Query (Full only - always full reload)
-- Source: PRODDTA.F00192 (direct table, not view)
-- Destination: FunctionCode
-- Schedule: Mass/Daily/Hourly (all with PrepurgeData=true)
-- Note: No filtered variant exists. Always performs full reload.
-- Note: Uses LISTAGG to combine multi-row descriptions into single row per code.

SELECT Code,
       TRIM(LISTAGG(Description, ' ') WITHIN GROUP(ORDER BY Description) || CASE WHEN MAX(total_lengthb) > 4000 THEN '...' ELSE '' END) Description,
       SYSDATE AS LastUpdateDT
FROM (
    SELECT TRIM(fc.CFKY) AS Code,
           TRIM(ASCIISTR(fc.CFDS80)) AS Description,
           SUM(LENGTHB(TRIM(fc.CFDS80))+1) OVER(PARTITION BY TRIM(fc.CFKY) ORDER BY TRIM(fc.CFDS80)) - 1 cumul_lengthb,
           SUM(LENGTHB(TRIM(fc.CFDS80))+1) OVER(PARTITION BY TRIM(fc.CFKY)) - 1 total_lengthb,
           COUNT(*) OVER(PARTITION BY TRIM(fc.CFKY)) num_values
    FROM   PRODDTA.F00192 fc
    WHERE  TRIM(fc.CFKY) IS NOT NULL
)
WHERE total_lengthb <= 4000 OR cumul_lengthb <= 4000 - length('...')
GROUP BY Code
