我寫了一個查詢中獲得:如何逗號分隔的多個行從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個不同的記錄針對單個客戶端。請告訴我如何將這三條記錄轉換爲同一列中的單個逗號分隔記錄,並將這三行記錄成單行。
請保存圖像,然後看。可能是這裏不可見!
爲什麼不從查詢中返回多個表並在asp.net中建立它們之間的關係? – 2011-04-04 09:39:21
不,我不能。我只需要這個Ehtnicity逗號分隔。這是要求。 – asma 2011-04-04 09:43:40