Concatenate one field after GROUP BY

后端 未结 1 1789
猫巷女王i
猫巷女王i 2020-12-06 08:34

This question have been asked many times in SO but none of the answers is satisfying to my situation.

  1. Question 1
  2. Question 2
  3. Question 3
  4. <
相关标签:
1条回答
  • 2020-12-06 09:23

    Try this:

    SELECT [CreatedTime]
          ,[DeletedTime]
          ,[DeletedBy]
          ,[ResourceId]
          ,[AccountId]
          ,STUFF((select ',' + raType.Type 
                  from vwAllAssignments raType 
                  where raType.AccountId = vwAllAssignments.AccountId and 
                        raType.ResourceId = vwAllAssignments.ResourceId and 
                        raType.DeletedBy is null 
                  for xml path('')), 1,1,'') AS [Type]
    FROM vwAllAssignments
    GROUP BY [ResourceId], [AccountId], [CreatedTime], [DeletedTime], [DeletedBy]
    

    And an index like this should be helpful.

    create index IX_vwAllAssignments on vwAllAssignments(AccountId, ResourceId, DeletedBy) include(Type)
    
    0 讨论(0)
提交回复
热议问题