2014-01-30 42 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 Server的情况下看这里:http://stackoverflow.com/questions/17591490/how-to-make-a-query-with-group-concat-in-sql-server –

+0

我有这个声明,但我(SELECT N';'+ U.email FROM dbo.Users AS U LEFT OUTER JOIN(SELECT up.UserID,MAX(CASE WHEN ppd.PropertyName ='Committee'THEN up.PropertyValue ELSE''END)AS Committee FROM dbo.UserProfile AS up INNER JOIN dbo.ProfilePropertyDefinition AS ppd ON up.PropertyDefinitionID = ppd.PropertyDefinitionID and ppd.PortalID = 0 Group By up.UserID)as upd U.UserID = upd.UserID其中upd.Committee ='Beach and Recreation'FOR XML PATH(''),TYPE).value('text()[1]','nvarchar(max)'),1, 2,N'')as QuestionOptionValue – user3223048

回答

0

我eneded了想出来这就是:

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 
1

在MySQL的情况下,你可以使用下面的查询

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

演示在http://sqlfiddle.com/#!2/db6b5a/2