本文介绍了将多行连接成一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好 我有以下表格: FileKey Title FirstName MiddleName LastName 1 Tim Adikari先生 2 Sue Andrew女士 3 Mathew William Anyalai先生 3 Elaine June Blazejczyk女士 4 Scott Alfred Bond先生 4 Tony Bromwich先生 我需要根据filekey栏产生以下结果 1-Tim Adikari 2-Sue Andrew 3-Mathew William Anyalai,Elaine June Balzejczyk 4-Scott Alfred Bond,Tony Bromwich 我怎么能这样做?任何人帮助 谢谢 NasifHi AllI have a table of the following:FileKeyTitleFirstNameMiddleNameLastName1MrTimAdikari2MsSueAndrew3MrMathew William Anyalai3MrsElaine June Blazejczyk4MrScott Alfred Bond4MrTonyBromwichI need to produce result of the following based on the filekey column1-Tim Adikari2-Sue Andrew3-Mathew William Anyalai,Elaine June Balzejczyk4-Scott Alfred Bond,Tony BromwichHow can I do that? Anyone helpThanksNasif推荐答案select name + '' + lastname from emplyeeDECLARE @tbl as table (FileKey nvarchar(10),Title nvarchar(10),FirstName nvarchar(10),MiddleName nvarchar(10),LastName nvarchar(10))INSERT INTO @tblSELECT '1','Mr','Tim','','Adikari'UNIONSELECT '2', 'Ms','Sue','','Andrew'UNIONSELECT '3', 'Mr','Mathew','William','Anyalai'UNIONSELECT '3', 'Mrs','Elaine','June','Blazejczyk'UNIONSELECT '4', 'Mr','Scott','Alfred','Bond'UNIONSELECT '4', 'Mr','Tony','','Bromwich'SELECT p1.FileKey, ( SELECT Title + ' ' + FirstName + ' ' + MiddleName + ' ' + LastName + ',' FROM @tbl p2 WHERE p2.FileKey = p1.FileKey ORDER BY FileKey FOR XML PATH('') ) AS Products FROM @tbl p1 GROUP BY FileKey ; 替换Replace@tbl在上面查询您的实际表名。 in above query with your actual table name. 这篇关于将多行连接成一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-18 19:55