我的查询几乎可以满足我的需求:
SET NOCOUNT ON
SELECT D.Id from MtdMdl_HierarchicalObject D
where D.Name LIKE '%R%'
SELECT B.* FROM MtdMdl_Item A
OUTER apply (SELECT * FROM MtdMdl_Item as C where a.Id=c.Id) as B
WHERE A.Owner in
(
SELECT D.Id from MtdMdl_HierarchicalObject D
where D.Name LIKE '%R%'
)
order by Id offset 0 ROWS FETCH NEXT 3 ROWS ONLY
GO
不幸的是,我希望Order By Id Fetch New 3 Rows应用于返回的每个对象:
SELECT D.Id from MtdMdl_HierarchicalObject D
where D.Name LIKE '%R%'
因为当前脚本对前一个语句的整个结果应用了Paging。
答案 0 :(得分:1)
如果您没有跳过任何行而只是获得3行,为什么还要使用OFFSET呢?只使用TOP子句
SELECT B.*
FROM MtdMdl_Item A OUTER APPLY
(SELECT TOP 3 *
FROM MtdMdl_Item
where Id= a.Id) as B
WHERE A.Owner in
(
SELECT D.Id from MtdMdl_HierarchicalObject D
where D.Name LIKE '%R%'
)
order by Id
在这种情况下,你可以做这样的事情......
SELECT * FROM
(
SELECT B.*, A.ID , RN = ROW_NUMBER() OVER (PARTITION BY A.ID ORDER BY A.ID)
FROM MtdMdl_Item A OUTER APPLY
(SELECT *
FROM MtdMdl_Item
where Id= a.Id) as B
WHERE A.Owner in
(
SELECT D.Id from MtdMdl_HierarchicalObject D
where D.Name LIKE '%R%'
)
order by Id
) Q
WHERE RN >= LowerList AND RN <= UpperLimit