It Ninja Turtle One sentence SQL statement--connect all values of the same field

Source: Internet
Author: User

Oracle can use Sys_connect_by_path string aggregation functions: SELECT LTRIM (MAX (Sys_connect_by_path (ProductName, ', ')), ', ') as Productnamefrom (SELECT ' 1 ' as ID, ProductName, row_number () over (PARTITION by ' 1 ' ORDER by ProductName) as cur  R, Row_number () over (PARTITION by ' 1 ' ORDER by ProductName)-1 as Prev from products--where condition ORDER by ProductName) START with Curr = 1CONNECT by prev = PRIOR Curr and id = PRIOR idGROUP by ID But Ms SQL 2000 does not have this function, but you can use a custom function or procedure to  implementation.--Drop function Connectstringcreate function connectstring (@name varchar) RETURNS varchar (1024x768) as BEGIN declare @Str varchar Set @Str = "Select @Str = @Str + (case @Str =" then "Else", ' end) + [ProductName] from pro Ducts--where [ProductName] = @name return @Str END go--call select DISTINCT dbo. ConnectString (ProductName) from Productssql Server 2005 available outer APPLY: The following is an excerpt from the Web:--Sample data declare @t TABLE (id int, value VARC Har (Ten)) INSERT @t Select 1, ' AA ' union ALL select 1, ' BB ' union ALL Select 2, ' AAA ' union All select 2, ' BBB ' UNION all select 2, ' CCC '--Query processing Select *FROM (select DISTINCT ID from @t) aouter APPLY ( SELECT [values]= STUFF (replace (replace (SELECT value from @t N WHERE id = a.id for XML AUTO), ' <n value= ' ', ', '), '/> ', '), 1, 1, ') ' n/*--result ID val UEs---------------------------1 aa,bb2 AAA,BBB,CCC (2 rows affected)--*/
2 admin/adminy/Liu Sunan/Zhang Weiru/Tanhui/
3 Wang Yiping/Lin Lin/Yang Yi/Operations Management Unit asset Manager/Liu Ting/Zhongyuan/Dujia/
4 Branch Sales Department computer administrator a corner/branch sales office computer Administrator B Corner/tingting Zhang/anti-money laundering 301/Credit Synthesis 301/Chen Guang/蒋喆/quiet/single loan 301/Tan Liwei/Met/Branch sales Office asset manager/Founder/Li Chen/Sales Department planning Contact/Li Yu/
5 Li Juan/Shenhua Sub-branch computer administrator a corner/Shenhua branch computer Administrator B Corner/Wang/anti-money laundering 302/Credit Synthesis 302/Zhe Zhang/Zhou Xiaozhou/Single loan 302/Wang Xiao/Shenhua Branch asset manager/Yang Houxian/Shenhua Project Contact/
6 Hua Branch computer administrator a corner/Mr Anwar sub-branch computer Administrator B Corner/Wei Hong/Anti-money Laundering 303/Credit Synthesis 303/organizations/Xu Yue Kun/Chen Yu/WAN Xia/single loan 303/garden Road Sub-branch asset manager/Choshi/Chengyao/Garden Road Planning Contact/
7 West Third Ring sub-branch computer Administrator B Corner/West Third ring sub-branch computer administrator a corner/Zhang Yongfing/Anti-money laundering 304/Credit Synthesis 304/Dengli/each credit 304/Zhang Jihong/Shing/Li Dongdong (304)/Mugongjixiean/West Third ring sub-branch asset manager/Zhou Qi/lifei/West Third Ring project contact/Zhang Xin/
8 Dong Junyu/Zhongguancun Sub-branch computer Administrator B Corner/Zhongguancun Branch computer administrator a corner/Li Xin/anti-money laundering 305/Credit Synthesis 305/A loan order 305/Xu Wenwen/Dan Rui/Zhongguancun Sub-branch asset manager/Jia Jinping/Zhang Ying 01/car handsome/zhongguancun planning contact/

It Ninja Turtle One sentence SQL statement--connect all values of the same field

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.