Query shards and combinations in Oracle

Source: Internet
Author: User

Table: msg_content

Table: msg_contact_person


Expected results


Key Point Analysis: the difference between the result table and table 1 is that the real name of table 2 is added to the "er" field.

Implementation Method:

Step 1: Split the fields in table 1

SELECT c. msg_content_id AS contentId,
REGEXP_SUBSTR (receiver, '[^;] +', 1, l) AS B
FROM msg_content c,
(Select level l
FROM DUAL
Connect by level <= 30)
WHERE l <= LENGTH (worker ER )-
LENGTH (REPLACE (separator er, ';') + 1

Result set:

Step 2: left join with table 2:

Select bb. contentId, bb. B, dd. user_real_name
From (SELECT c. msg_content_id AS contentId,
REGEXP_SUBSTR (receiver, '[^;] +', 1, l) AS B
FROM msg_content c,
(Select level l
FROM DUAL
Connect by level <= 30)
WHERE l <= LENGTH (worker ER )-
LENGTH (REPLACE (separator er, ';') + 1) bb
Left join msg_contact_person dd on bb. B = dd. account_name
Where bb. B is not null

Result set:

Step 4: Use wm_concat to combine characters and add the page OK

Select *
From (select dd. msg_content_id,
Dd. account_name,
Cc. Cycler,
Dd. sender,
Dd. realname,
Dd. title,
Dd. content,
Dd. send_time,
Dd. if_del,
Dd. msg_lv
From msg_content dd,
(Select jj. contentId,
Replace (wm_concat (jj. B |
Decode (jj. user_real_name,
'',
'',
'(' | Jj. user_real_name | ')')),
',',
';') | ';' Worker er
From (select bb. contentId, bb. B, dd. user_real_name
From (SELECT c. msg_content_id AS contentId,
REGEXP_SUBSTR (receiver, '[^;] +', 1, l) AS B
FROM msg_content c,
(Select level l
FROM DUAL
Connect by level <= 30)
WHERE l <= LENGTH (worker ER )-
LENGTH (REPLACE (separator er, ';') + 1) bb
Left join msg_contact_person dd on bb. B = dd. account_name
Where bb. B is not null) jj
Group by jj. contentId) cc
Where dd. msg_content_id = cc. contentId
Order by dd. msg_content_id desc)
Where rownum <= 10

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.