如何逗号分隔从SQL查询获取的多个行 [英] How to Comma separate multiple rows obtained from a SQL Query

查看:154
本文介绍了如何逗号分隔从SQL查询获取的多个行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个查询:

select tbl1.Id, tbl1.FirstName, tbl1.MiddleInit, tbl1.LastName, tbl1.SocialSecNum, tbl1.DateOfBirth, 
tbl1.EyeColor, tbl1.Sex, tbl1.AlertNotes, tbl1.RiskNotes, tbl1.Height, tbl1.[Weight], tbl1.AllergyNotes, 
tbl2.HairColor, tbl3.SexualConsent, tbl4.MaritalStatus, tbl5.Ethnicity, tbl6.Veteran, tbl7.Religion, tbl8.Race, 
tbl9.[Language] as [Language]

from

(SELECT C.Id, C.FirstName, C.MiddleInit, C.LastName, C.SocialSecNum, C.DateOfBirth, C.Sex, 
GL.LookupItem as EyeColor, CC.AlertNotes, CC.RiskNotes, CC.Height, CC.[Weight], CC.AllergyNotes  
FROM dbo.Client C INNER JOIN dbo.ClientCharacteristic CC ON C.Id = CC.ClientId INNER JOIN dbo.GeneralLookup GL ON 
GL.Id=CC.glEyeColorId) tbl1,  

(SELECT GL.LookupItem as HairColor  
FROM dbo.ClientCharacteristic CC INNER JOIN dbo.GeneralLookup GL ON 
GL.Id=CC.glHairColorId) tbl2,

(SELECT GL.LookupItem as SexualConsent  
FROM dbo.ClientCharacteristic CC INNER JOIN dbo.GeneralLookup GL ON 
GL.Id=CC.glSexConsentId) tbl3,

(SELECT GL.LookupItem as MaritalStatus  
FROM dbo.Client C INNER JOIN dbo.GeneralLookup GL ON 
GL.Id=C.glMaritalStatusId where C.Id=2) tbl4,

(SELECT GL.LookupItem as Ethnicity 
FROM dbo.GeneralLookupTransition GLT INNER JOIN dbo.GeneralLookup GL ON 
GL.Id=GLT.glValueId where GLT.ParentRecordId=2 and GLT.ControlName='CONSUMER_ETHNICITY_LIST') tbl5, 

(SELECT GL.LookupItem as Veteran  
FROM dbo.Client C INNER JOIN dbo.GeneralLookup GL ON 
GL.Id=C.glVeteranId where C.Id=2) tbl6, 

(SELECT GL.LookupItem as Religion 
FROM dbo.GeneralLookupTransition GLT INNER JOIN dbo.GeneralLookup GL ON 
GL.Id=GLT.glValueId where GLT.ParentRecordId=2 and GLT.ControlName='CONSUMER_RELIGION_DROPDOWN') tbl7, 

(SELECT GL.LookupItem as Race 
FROM dbo.GeneralLookupTransition GLT INNER JOIN dbo.GeneralLookup GL ON 
GL.Id=GLT.glValueId where GLT.ParentRecordId=2 and GLT.ControlName='CONSUMER_RACE_DROPDOWN') tbl8, 

(SELECT GL.LookupItem as [Language] 
FROM dbo.GeneralLookupTransition GLT INNER JOIN dbo.GeneralLookup GL ON 
GL.Id=GLT.glValueId where GLT.ParentRecordId=2 and GLT.ControlName='CONSUMER_CHARACTERISTIC_LANGUAGE_DROPDOWN') tbl9

结果是:

这些查询中得到的一些列。请参阅列种族。
它对单个客户端有3个不同的记录。请告诉我如何在同一列中的单个逗号分隔记录中转换这三条记录,这3行将成为单行。

These some of the columns I got from this query. See the column Ethnicity. It has 3 different records against a single client. Please tell me how can I convert these three records in a single comma separated records in the same column and these 3 rows become a single row.

请保存图像,然后看到。可能在这里不可见!

Please save the image and then see. May be it is not visible here!

推荐答案

您的查询对我来说太多了。但这里是问题的答案...

Your query is a bit too much for me to process. But here is the answer to the question...


如何逗号分隔多个从SQL查询获得的行

How to Comma separate multiple rows obtained from a SQL Query

您可以使用来表示xml路径('')

declare @T table (GroupID int, Value varchar(10))

insert into @T values (1, 'Row 1')
insert into @T values (1, 'Row 2')
insert into @T values (2, 'Row 3')
insert into @T values (2, 'Row 4')
insert into @T values (2, 'Row 5')

select GroupID,
  stuff(
  (select ','+Value as '*'
   from @T as T2
   where T2.GroupID = T1.GroupID
   for xml path('')), 1, 1, '') as [Values]
from @T as T1
group by GroupID  

结果

GroupID     Values
----------- ------------------
1           Row 1,Row 2
2           Row 3,Row 4,Row 5

这篇关于如何逗号分隔从SQL查询获取的多个行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆