如何在同一行上组合多行数据

时间:2011-12-08 19:22:48

标签: sql sql-server sql-server-2008 tsql

基本上我想为具有匹配activityid的所有行获取所有名称(RequiredAccount,RequiredContact,RequiredOwner)并将它们放在自己的列中。简而言之,我想获取多行数据并将它们放在一个列中。基本上我想做这个人正在做的事情(http://www.sqlprof.com/blogs/sqldev/archive/2008/03/31/how-to-list-multiple-rows-of-data-on-same -line.aspx)但我似乎无法让它发挥作用。

SELECT DISTINCT 
    Appointment.ActivityId, Appointment.ScheduledStart, Appointment.OwnerIdName, Contact.AccountIdName, Appointment.new_ContactPersonName, 
        Appointment.Subject, Appointment.new_ColderNotes, Account.AccountId, Contact_1.ContactId, SystemUser.SystemUserId, SystemUser.FullName AS OptionalOwner, 
        Contact_1.FullName AS OptionalContact, Account.Name AS OptionalAccount, ActivityParty.PartyId, ActivityParty.ParticipationTypeMask, 
        Contact_1.FullName AS RequiredContact, Account.Name AS RequiredAccount, SystemUser.FullName AS RequiredOwner, Account.new_BusinessUnit
FROM
    Contact AS Contact_1 RIGHT OUTER JOIN
        Account RIGHT OUTER JOIN
        SystemUser RIGHT OUTER JOIN
        Appointment INNER JOIN
        ActivityParty ON Appointment.ActivityId = ActivityParty.ActivityId ON SystemUser.SystemUserId = ActivityParty.PartyId ON Account.AccountId = ActivityParty.PartyId ON
        Contact_1.ContactId = ActivityParty.PartyId LEFT OUTER JOIN
        Contact ON Appointment.new_ContactPerson = Contact.ContactId

如果ActivityID匹配,如果三个必需列位于一列中,我想要的是什么。所以前两行在新列中有两个名称。

任何想法,帮助等等。

非常感谢!

1 个答案:

答案 0 :(得分:0)

Simulating group_concat in sql server 2005

该线程中有几个工作示例,但它们都不是很漂亮: - /