Is there a way we can get previous row data in present row in SQL Server
For eg I have a table as
id | name
1 | abc
2 | def
3 | xyz
I need to query data in the following manner .. so as to concatenate previous row with present one
This way
id | name
1 | abc
2 | abcdef
3 | abcdefxyz
I tries with lag() but, couldn't figure out what am I doing wrong. I am very new in SQL Server.. I am not aware of other functions that could help
Thanks in advance
CodePudding user response:
you can use String_agg function like below
create table t (id int, name nvarchar(max));
insert into t values (1 , 'abc'),(2, 'def'),(3,'xyz');
select t1.id,string_agg(t2.name,'') from t t1 left join t t2
on t1.id>=t2.id
group by t1.id