Oracle-SQL skills (2) Use commas (,) to implement the WMSYS. WM_CONCAT function for multi-row records

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.