hi sqlor
hope this helps
grouping by id is working ,,have to work on getting Order By Sequence
sample data
drop table Table1
create table Table1
(
Status varchar(10) ,
RecordDate date ,
ID int ,
StatusDate date
)
go
set dateformat ymd
insert into Table1(ID,RecordDate,Status,StatusDate) select 1,'2019-12-31','OK2' ,'2020-09-10'
insert into Table1(ID,RecordDate,Status,StatusDate) select 1,'2019-12-25','OK1' ,'2020-09-09'
insert into Table1(ID,RecordDate,Status,StatusDate) select 1,'2019-12-22','OK3' ,'2020-09-12'
insert into Table1(ID,RecordDate,Status,StatusDate) select 2,'2019-11-30','Nope','2020-09-11'
insert into Table1(ID,RecordDate,Status,StatusDate) select 3,'2019-10-24','Soup','2020-09-12'
SELECT
xx.id
, STRING_AGG(CONVERT(NVARCHAR(max), xx.status), ';') AS [Status Sequence]
from
( select distinct B.id,b.status FROM Table1 B INNER JOIN Table1 A ON B.Id = A.Id ) xx
group by
xx.id ;
Now i have the order by working
; with cte as
(
select distinct top 100 B.id,b.status FROM Table1 B INNER JOIN Table1 A ON B.Id = A.Id order by b.status desc
)
SELECT
xx.id
, STRING_AGG(CONVERT(NVARCHAR(max), xx.status), ';') AS [Status Sequence]
from
cte xx
group by
xx.id ;