insert into tmp_product select 'A','ProductA'||rownum from dual connect by level < 100 union all select 'B','ProductB'||rownum from dual connect by level < 300 union all select 'C','ProductC'||rownum from dual connect by level < 400 union all select 'D','ProductD'||rownum from dual connect by level < 500 union all select 'E','ProductE'||rownum from dual connect by level < 600;
复制代码 代码如下:
SELECT PRODUCT_TYPE, WM_CONCAT(PRODUCT_NAME) || MAX(STR) AS PRODUCT_MULTI_NAME FROM (SELECT PRODUCT_TYPE, PRODUCT_NAME, CASE WHEN ALL_SUM > 4000 THEN '...' ELSE NULL END AS STR FROM (SELECT PRODUCT_TYPE, PRODUCT_NAME, SUM(VSIZE(PRODUCT_NAME || ',')) OVER(PARTITION BY PRODUCT_TYPE) AS ALL_SUM, SUM(VSIZE(PRODUCT_NAME || ',')) OVER(PARTITION BY PRODUCT_TYPE ORDER BY PRODUCT_NAME) AS UP_SUM FROM TMP_PRODUCT) WHERE (UP_SUM <= 3998 AND ALL_SUM > 4000) OR ALL_SUM <= 4001) GROUP BY PRODUCT_TYPE