如何让 select 与除最后一列不同的所有内容和最后一列作为逗号分隔值

How to have select distinct of all but last column , and last column as comma separated values

我有一个具有以下结构的 table。它有 8 列,其中第 7 列有时包含重复项,最后一列可以具有不同的值(虽然没有唯一值)。

如何 select 区分前 7 列,然后将最后一列显示为逗号分隔值。

所以最后一列应该如下所示,

Select 子查询中不同的 7 个值,然后执行 XML STUFF 来合并这些值

请注意 "--在此处添加您的其他字段"

例子

Select A.*
      ,CollectionDate  = Stuff((Select Distinct ',' +cast(CollectionDate as varchar(max))
                                 From  YourTable 
                                 Where Quantity=A.Quantity 
                                   and Protein =A.Protein
                                   and Carb    =A.Carb
                                   -- Add your other fields here
                                 For XML Path ('')),1,1,'') 
 From (Select Distinct 
              Quantity
             ,Protein
             ,Carb
             -- Add your other fields here
        From  YourTable ) A