Ramco Ramco

Ramco Ramco

  • 445
  • 3.4k
  • 530.6k

multiple row data in pivot

May 18 2023 10:18 AM

Hi

  If for same bookid there r 2 records , is it possible that in Status column it should display values with comma separation

Below is the Pivot table

select @cols = @cols + QUOTENAME(StudentName) + ',' from (Select distinct StudentName from #finalTable where StudentID in (select [value] from string_split(@StudentID,','))) as tmp
	select @cols = substring(@cols,0,len(@cols))
	set @query = 'select [BookId] ,BookTitle [Book],[ComboBookName],[CoreRPL],[Lexile],[NumberOfPages],[SessionPlanStatus], ' + @cols + ' from (select [BookID] ,BookTitle,[ComboBookName],[CoreRPL],[Lexile],[NumberOfPages],[SessionPlanStatus],Status,StudentName from #finaltable where (('''+@BookID+'''=''0'' OR [BookId] IN('+ cast(@BookID as varchar(Max))+')))) x pivot
	( Max(Status) for StudentName in (' + @cols + ')) piv order by BookTitle';

Thanks


Answers (1)