2014-01-30 5 views
0

이것은 쉬운 것이어야합니다. 여기 내 문입니다 :Sql 두 개의 셀을 결합하십시오.

Select UP1.PropertyValue as QuestionOption, email +'; ' as QuestionOptionValue 
    from Users 
INNER JOIN UserProfile UP1 
on UP1.UserId = Users.UserId AND PropertyDefinitionID = (SELECT PropertyDefinitionID 
    FROM ProfilePropertyDefinition Where PropertyName='Committee' and 
    PropertyValue='Beach and Recreation' AND PortalID=0) 

그리고 반환

QuestionOption   QuestionOptionValue 
Beach and Recreation  [email protected]; 
Beach and Recreation  [email protected]; 

가 나는

QuestionOption   QuestionOptionValue 
Beach and Recreation  [email protected]; [email protected] 

어떻게 추가 할 필요가 어디 않습니다 돌아가려면?

+1

당신의 RDBMS는? –

+0

SQL 서버의 경우 다음을보십시오. http://stackoverflow.com/questions/17591490/how-to-make-a-query-with-group-concat-in-sql-server –

+0

이 진술서를 가지고 있지만 선택을 반으로 줄여야합니다 : SELECT 'Beach and Recreation'을 QuestionOption으로 설정하십시오. ((SELECT N ';'+ U.email FROM dbo.Users from U 좌측 바깥 쪽 조인 (SELECT .UserID, MAX (ppd.PropertyName = 'Committee'THEN up.PropertyValue ELSE''END) AS위원회 dbo.UserProfile부터 위로 INNER JOIN dbo.ProfilePropertyDefinition AS ppd ON up.PropertyDefinitionID = ppd.PropertyDefinitionID 및 ppd.PortalID = 0 Group by up.UserID) as upd ('텍스트() [1]', 'nvarchar (최대)'), 1, 2, 3, 2, N '') QuestionOptionValue – user3223048

답변

0

에서 다음 쿼리

Select 
    QuestionOption, GROUP_CONCAT(QuestionOptionValue SEPARATOR ' ') as QuestionOptionValue 
FROM 
    Table1 
GROUP BY 
    QuestionOption 

데모를 사용할 수 있습니다

SELECT 'Beach and Recreation' as QuestionOption, Stuff((SELECT N'; '+U.email FROM 
Users AS U JOIN UserProfile ON U.UserID = UserProfile.UserID 
where UserProfile.PropertyValue='Beach and Recreation' FOR XML 
PATH(''),TYPE).value('text()[1]','nvarchar(max)'),1,2,N'') as QuestionOptionValue 
union SELECT 'CC/Web Page' as QuestionOption, Stuff((SELECT N'; '+U.email FROM Users 
AS U JOIN UserProfile ON U.UserID = UserProfile.UserID 
where UserProfile.PropertyValue='CC/Web Page' FOR XML PATH(''),TYPE).value('text()  
[1]','nvarchar(max)'),1,2,N'') as QuestionOptionValue union 
SELECT 'Civic Association Board' as QuestionOption, Stuff((SELECT N'; '+U.email FROM 
Users AS U JOIN UserProfile ON U.UserID = UserProfile.UserID 
where UserProfile.PropertyValue='Civic Association Board' FOR XML 
PATH(''),TYPE).value('text()[1]','nvarchar(max)'),1,2,N'') as QuestionOptionValue union 
SELECT 'Clubhouse Rentals' as QuestionOption, Stuff((SELECT N'; '+U.email FROM Users 
AS U JOIN UserProfile ON U.UserID = UserProfile.UserID 
where UserProfile.PropertyValue='Clubhouse Rentals' FOR XML 
PATH(''),TYPE).value('text()[1]','nvarchar(max)'),1,2,N'') as QuestionOptionValue union 
SELECT 'Fundraising Committee' as QuestionOption, Stuff((SELECT N'; '+U.email FROM 
Users AS U JOIN UserProfile ON U.UserID = UserProfile.UserID 
where UserProfile.PropertyValue='Fundraising Committee' FOR XML 
PATH(''),TYPE).value('text()[1]','nvarchar(max)'),1,2,N'') as QuestionOptionValue 
Union SELECT 'Lake Preservation' as QuestionOption, Stuff((SELECT N'; 
'+U.emailFROM  
Users AS U JOIN UserProfile ON U.UserID = UserProfile.UserID 
where UserProfile.PropertyValue='Lake Preservation' FOR XML 
PATH(''),TYPE).value('text()[1]','nvarchar(max)'),1,2,N'') as QuestionOptionValue 
Union SELECT 'Lifeguard Committee' as QuestionOption, Stuff((SELECT N'; '+U.email 
FROM Users AS U JOIN UserProfile ON U.UserID = UserProfile.UserID 
where UserProfile.PropertyValue='Lifeguard Committee' FOR XML  
PATH(''),TYPE).value('text()[1]','nvarchar(max)'),1,2,N'') as QuestionOptionValue 
Union SELECT 'Night Watch' as QuestionOption, Stuff((SELECT N'; '+U.email FROM Users 
AS U JOIN UserProfile ON U.UserID = UserProfile.UserID 
where UserProfile.PropertyValue='Night Watch' FOR XML PATH(''),TYPE).value('text() 
[1]','nvarchar(max)'),1,2,N'') as QuestionOptionValue Union 
SELECT 'Volunteer Committee' as QuestionOption, Stuff((SELECT N'; '+U.email FROM 
Users AS U JOIN UserProfile ON U.UserID = UserProfile.UserID 
where UserProfile.PropertyValue='Volunteer Committee' FOR XML 
PATH(''),TYPE).value('text()[1]','nvarchar(max)'),1,2,N'') as QuestionOptionValue 
관련 문제