我正在尝试派生一个分页逻辑。
我有以下字段:
RecordNo Lines
1 20
2 130
3 50
4 60
5 350
6 100
说我的页面大小是170行。
我想得到的结果是:
RecordNo Lines CumSum PageNo
1 20 20 1
2 130 150 1
3 50 50 2 (as cumulative sum 200 exceeds 170, reset to 0)
4 60 110 2
5 350 350 3 ((as cumulative sum 460 exceeds 170, reset to 0)
6 100 100 4 ((as cumulative sum 460 exceeds 170, reset to 0)
我可以使用游标来实现,但有没有办法只通过SQL实现它?
以下是OP发布的ddl和示例数据:
CREATE TABLE PAGING (RECORDNO INT, LINES INT );
INSERT INTO PAGING VALUES(1,20);
INSERT INTO PAGING VALUES(2,130);
INSERT INTO PAGING VALUES(3,50);
INSERT INTO PAGING VALUES(4,60);
INSERT INTO PAGING VALUES(5,350);
INSERT INTO PAGING VALUES(6,100);
更新
Zohar,感谢您对此进行调查。该查询与我提供的数据完美配合,但是当我扩展了更多数据时,由于页面基数不会超过170而移动,因此无法提供正确的结果。以下是我尝试使用SQL的数据:
INSERT [dbo].[PAGING] ([RECORDNO], [LINES]) VALUES (1, 20);
INSERT [dbo].[PAGING] ([RECORDNO], [LINES]) VALUES (2, 130);
INSERT [dbo].[PAGING] ([RECORDNO], [LINES]) VALUES (3, 50);
INSERT [dbo].[PAGING] ([RECORDNO], [LINES]) VALUES (4, 60);
INSERT [dbo].[PAGING] ([RECORDNO], [LINES]) VALUES (5, 350);
INSERT [dbo].[PAGING] ([RECORDNO], [LINES]) VALUES (6, 100);
INSERT [dbo].[PAGING] ([RECORDNO], [LINES]) VALUES (7, 20);
INSERT [dbo].[PAGING] ([RECORDNO], [LINES]) VALUES (8, 10);
INSERT [dbo].[PAGING] ([RECORDNO], [LINES]) VALUES (9, 20);
INSERT [dbo].[PAGING] ([RECORDNO], [LINES]) VALUES (10, 30);
INSERT [dbo].[PAGING] ([RECORDNO], [LINES]) VALUES (11, 5);
INSERT [dbo].[PAGING] ([RECORDNO], [LINES]) VALUES (12, 5);
INSERT [dbo].[PAGING] ([RECORDNO], [LINES]) VALUES (13, 5);
INSERT [dbo].[PAGING] ([RECORDNO], [LINES]) VALUES (14, 10);
INSERT [dbo].[PAGING] ([RECORDNO], [LINES]) VALUES (15, 205);
INSERT [dbo].[PAGING] ([RECORDNO], [LINES]) VALUES (16, 156);
INSERT [dbo].[PAGING] ([RECORDNO], [LINES]) VALUES (17, 5);
INSERT [dbo].[PAGING] ([RECORDNO], [LINES]) VALUES (18, 2);
INSERT [dbo].[PAGING] ([RECORDNO], [LINES]) VALUES (19, 7);
答案 0 :(得分:0)
<强>更新强>
以下是完整的解决方案and it's fiddle link:
WITH cte AS
(
SELECT RECORDNO,
LINES,
SUM(LINES) OVER (ORDER BY RECORDNO) CumSum,
SUM(LINES) OVER (ORDER BY RECORDNO) / 170 AS PageNumberBase
FROM PAGING
)
SELECT RECORDNO,
LINES,
SUM(LINES) OVER (PARTITION BY PageNumberBase ORDER BY RECORDNO) As CumSum,
DENSE_RANK() OVER(ORDER BY PageNumberBase) As PageNumber
FROM cte
ORDER BY RECORDNO
第一个版本:
使用dense_rank窗口功能和cte我能够产生非常接近的东西,只是cumSum并不完美。
不幸的是,我没有时间玩它更多,所以我将它留在这里,带有sqlFiddle的链接,希望OP或其他人能够完成解决方案:
WITH cte AS
(
SELECT RECORDNO,
LINES,
SUM(LINES) OVER (ORDER BY RECORDNO) CumSum,
SUM(LINES) OVER (ORDER BY RECORDNO) / 170 AS PageNumber
FROM PAGING
)
SELECT RECORDNO,
LINES,
PageNumber,
CumSum - (170 * PageNumber) As CumSum,
DENSE_RANK() OVER(ORDER BY PageNumber)
FROM cte