SQL does not repeatedly search for data and concatenates a column of multiple rows into one row. SQL multiple rows

Source: Internet
Author: User

SQL does not repeatedly search for data and concatenates a column of multiple rows into one row. SQL multiple rows

See the following table:

Table Name: Test

ID RowID Col1 Col2
1 1 A A
2 1 B A
3 1 A B
4 1 C B

 

 

 

 

 

1. The fields in the search table are repeated only once.

Select distinct Col1 from Test;

Result:

A

B

C

2. Count and query the quantity of this field

SELECT Col1, COUNT (Col1) FROM Test group by Col1;

Result:

A 2

B 1

C 1

3. Splice multiple rows in one column into one row.

Select STUFF (select ',' + F. col1 from Test F where F. rowID = 1 order by F. id for xml path (''), 1, 1,'') As Col3;

Result:

Col3

A, B, A, C

Related Article

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.