Sql组合两个单元格

时间:2014-01-30 10:37:59

标签: sql sql-server

我希望这应该是一个简单的问题。                     以下是我的发言:

 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      eamil@gmail.com;
Beach and Recreation      email@AOL.COM; 

我希望它返回

QuestionOption            QuestionOptionValue
Beach and Recreation      eamil@gmail.com; email@AOL.COM

我需要添加什么以及在哪里?

2 个答案:

答案 0 :(得分:1)

如果是MYSQL,您可以使用以下查询

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

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

答案 1 :(得分:0)

我最终搞清楚这是:

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