XML, 4 . ... .
with SRC as
(
select 'A' grp, 'abc' rec
union select 'A', 'def'
union select 'A', 'ghi'
union select 'A', 'jkl'
union select 'B', 'mno'
union select 'B', 'pqr'
)
select (
select ','+S2.X.value('.', 'nvarchar(max)')
from (
select top(4) S.rec, '...' as eli
from SRC as S
where S.grp = tableA.grp
for xml path('X'), type
) as S1(X)
cross apply S1.X.nodes('(X[position() lt 4]/rec, X[position() eq 4]/eli)/text()') as S2(X)
for xml path(''), type
).value('substring(text()[1], 2)', 'nvarchar(max)')
from (select 'A') as tableA(grp);
?
XML, .
select top(4) S.rec, '...' as eli
from SRC as S
where S.grp = tableA.grp
for xml path('X'), type
<X>
<rec>abc</rec>
<eli>...</eli>
</X>
<X>
<rec>def</rec>
<eli>...</eli>
</X>
<X>
<rec>ghi</rec>
<eli>...</eli>
</X>
<X>
<rec>jkl</rec>
<eli>...</eli>
</X>
XML nodes()
S1.X.nodes('(X[position() lt 4]/rec, X[position() eq 4]/eli)/text()') as S2(X)
X[position() lt 4]/rec rec X, X[position() eq 4]/eli eli .
XML .
abc
def
ghi
...
XML for xml path , substring(text()[1], 2) values(), .
:
row_number() , , a. , order by row_number(), , , .
with SRC as
(
select 'A' grp, 'abc' rec
union select 'A', 'def'
union select 'A', 'ghi'
union select 'A', 'jkl'
union select 'B', 'mno'
union select 'B', 'pqr'
)
select (
select top(4) ','+case when row_number() over(order by S.rec) = 4 then '...' else S.rec end
from SRC as S
where S.grp = tableA.grp
order by S.rec
for xml path(''), type
).value('substring(text()[1], 2)', 'nvarchar(max)')
from (select 'A') as tableA(grp);