1

I referenced This and others stacks

This is what I have done:

SELECT 
    ReportDate,  
    ReportID = STUFF((SELECT ',' + CAST(t1.ReportID AS varchar(50)) 
                      FROM BackEndEfficiency t1
                      WHERE t1.ReportID = t2.ReportID
                      FOR XML PATH ('')), 1, 1, '') 
FROM
    BackEndEfficiency t2
GROUP BY
    ReportDate

Error:

Column 'BackEndEfficiency.ReportID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

Why am I getting this error as I want the ReportDate to be grouped by?

Example of data structure

ReportDate  ReportID
--------------------
2020-03-11  30
2020-03-11  31
2020-03-16  32
2020-03-16  33

I would like to get this output:

ReportDate  ReportID
--------------------
2020-03-11  30,31
2020-03-16  32,33
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Chung Yau
  • 97
  • 1
  • 9

1 Answers1

0

I think you want ReportDate for the correlation clause. I would recommend:

select bee.ReportDate,
       stuff((select ',' + CAST(t1.ReportID as varchar(50)) 
              from BackEndEfficiency bee2
              where bee2.ReportDate = bee.ReportDate
              for xml path ('')
             ), 1, 1, '') 
from (select bee.ReportDate
      from BackEndEfficiency bee
     ) bee;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786