--Create tables
if OBJECT_ID('TestValues','U') is not null
begin
drop table TestValues
end
Create table TestValues(Id int identity(1,1),Name nvarchar(255),Tag varchar(25))
truncate table TestValues
declare @maxRows int=100
declare @Tag varchar(25)
while(@maxRows>0)
begin
set @maxRows-=1;
set @Tag=case when @maxRows % 2=0 then 'Yes' else 'No' end
insert into TestValues(Name,Tag) values ( CONVERT(varchar(20),@maxRows)+'_'+ convert(varchar(36),NEWID()),@Tag)
end --1. 分页。 Pagination
declare @PageSize int=15,@PageNumber int=1,@Offset int
--计算OFFSET值,即需要跳过的行数
set @Offset=(@PageNumber-1)*@PageSize
select Id,Name
from TestValues with (nolock)
where Id<50 --根据需要进行条件过滤
order by Name asc -- 根据需要进行排序
OFFSET @Offset rows -- 跳过前面的@Offset行
fetch next @PageSize rows only-- 然后取接下来的@PageSize行
--2. 行转列
select t.Tag,
Name= STUFF(( SELECT '; ' + m.Name
from TestValues m
where m.Tag=t.Tag group by m.Name
FOR XML PATH(''),TYPE
).value('.','nvarchar(max)'), 1, 1, '')
from TestValues t with (nolock)
where Id<50 group by t.Tag