Because there are many transaction records in the business system, some supervisors often need to see all the records and do not want to scroll. Next we will introduce the WMSYS function that comes with Oracle. WM_CONCAT, splicing, interested friends can understand
Because there are many transaction records in the business system, some supervisors often need to see all the records and do not want to scroll. Next we will introduce the WMSYS function that comes with Oracle. WM_CONCAT, splicing, interested friends can understand
Requirement:
Currently, due to the large number of transaction records in the business system, some supervisors often need to see all the records, but do not want to scroll. They can see all the records at a glance, so I thought of the form of String concatenation.
Solution: Concatenates data using the WMSYS. WM_CONCAT function provided by Oracle.
Function restrictions: Its output cannot exceed 4000 bytes.
To prevent SQL errors and meet the business needs, use "..." for more than 4000 bytes.
The SQL implementation is as follows::
The Code is as follows:
Create table TMP_PRODUCT
(PRODUCT_TYPE VARCHAR2 (255 ),
PRODUCT_NAME VARCHAR2 (255 ));
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;
The Code is as follows:
SELECT PRODUCT_TYPE,
WM_CONCAT (PRODUCT_NAME) | MAX (STR) AS PRODUCT_MULTI_NAME
FROM (SELECT PRODUCT_TYPE,
PRODUCT_NAME,
CASE
WHEN ALL_SUM & gt; 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