SQL stitching the same column values

Source: Internet
Author: User

In SQL, it is sometimes necessary to turn the value of a column into a row, such as the following data, what are the Zumenid with the same path?

A common practice can be found in this article http://www.cnblogs.com/xiashengwang/p/3503554.html

Here is another way to join the value of the concatenation column, using XML implementation. See this article http://www.cnblogs.com/yuefei/p/3799513.html

But this is a little bit more complicated and requires a subquery. SQL is as follows:

Select B.path,  stuff (replace (        (select Zumenid,path from t_zumen c where C.path = B.path FOR XML Auto), ' <c zumenid= "', ', '),        '" path= "' + b.path+ '"/> ', '), ","     ) zumenidsfrom (select distinct path from T_zumen) B--distinct the path first

The effect is as follows:

SQL stitching the same column values

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.