分页表另一个Sql问题
Sql Server 2005
表结构
CREATE TABLE [dbo].[Rate](
[RateID] [bigint] IDENTITY(1,1) NOT NULL,
[PairID] [bigint] NOT NULL,
[Open] [decimal](18, 4) NOT NULL,
[Close] [decimal](18, 4) NOT NULL,
[High] [decimal](18, 4) NOT NULL,
[Low] [decimal](18, 4) NOT NULL,
[Difference] [decimal](18, 4) NOT NULL,
[Average] [decimal](18, 4) NOT NULL,
[Percentage] [decimal](18, 4) NOT NULL,
[InfoDate] [datetime] NOT NULL,
[Hourly] [bit] NOT NULL,
[CaptureDateTime] [datetime] NULL,
CONSTRAINT [PK_Rate] PRIMARY KEY CLUSTERED
(
[RateID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
我正在使用分页来检索表
Select * from(
SELECT
(ROW_NUMBER()OVER (ORDER BY InfoDate ASC)) AS RowNo,
[RateID],
[PairID],
[Open],
[Close],
[High],
[Low],
[InfoDate],
[CaptureDateTime]
From Rate
) AS T
WHERE t.RowNo
BETWEEN 200*@PageNumber AND 200 * (@PageNumber+1)-1
ORDER BY RowNo DESC
[问题] 我需要查询,该查询将给出此表
PageNo、StartIndex(该页面中的第一个 RateId)、EndIndex(该页面中的最后一个 RateId)、StartDate(该页面中的第一个 infoDate)、EndDate(该页面中的最后一个 infoDate)。
Sql Server 2005
Table Structure
CREATE TABLE [dbo].[Rate](
[RateID] [bigint] IDENTITY(1,1) NOT NULL,
[PairID] [bigint] NOT NULL,
[Open] [decimal](18, 4) NOT NULL,
[Close] [decimal](18, 4) NOT NULL,
[High] [decimal](18, 4) NOT NULL,
[Low] [decimal](18, 4) NOT NULL,
[Difference] [decimal](18, 4) NOT NULL,
[Average] [decimal](18, 4) NOT NULL,
[Percentage] [decimal](18, 4) NOT NULL,
[InfoDate] [datetime] NOT NULL,
[Hourly] [bit] NOT NULL,
[CaptureDateTime] [datetime] NULL,
CONSTRAINT [PK_Rate] PRIMARY KEY CLUSTERED
(
[RateID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
I am using paging to retrive the table as such
Select * from(
SELECT
(ROW_NUMBER()OVER (ORDER BY InfoDate ASC)) AS RowNo,
[RateID],
[PairID],
[Open],
[Close],
[High],
[Low],
[InfoDate],
[CaptureDateTime]
From Rate
) AS T
WHERE t.RowNo
BETWEEN 200*@PageNumber AND 200 * (@PageNumber+1)-1
ORDER BY RowNo DESC
[Question]
I need query which will gives this me table
PageNo, StartIndex(first RateId in that page), EndIndex(Last RateId in that page), StartDate(first infoDate in that page), EndDate(Last infoDate in that page).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试执行以下操作:
两个 CTE 基本上与您执行的操作相同 - 它们提供数据分页。第二个 CTE
Pages
还提供每行的页码。从这些 CTE 中,我选择相关内容 - 页码(来自
Pages
),以及每个页面的第一个和最后一个RateID
和InfoDate
。这是有效的,因为:每个页面的第一行的行号比页面大小的倍数高 1,例如 1、201、401、601 等 - 因此整数除以页面大小的余数将是始终为 1
每页的最后一行都有一个可被页面大小整除的行号(例如 200、400、600 等),因此整数除法的余数为0
根据此信息,我可以从每个页面的
Pages
CTE 中挑选出第一个和最后一个RateID
和InfoDate
。You could try to do something like this:
The two CTE's basically do the same as you did - they provide paging for the data. The second CTE
Pages
additionally provides the page number for each row.From those CTE's, I select the relevant into - the page number (from
Pages
), and the first and lastRateID
andInfoDate
for each page. This works because:the first row of every page has a row number that's 1 above a multiple of your page size, e.g. 1, 201, 401, 601 etc. - so the remainder of an integer division by your page size will always be 1
the last row of every page has a row number that's divisible by your page size (e.g. 200, 400, 600 etc. ) and thus the remainder of the integer division is 0
Based on this information, I can pick out the first and last
RateID
andInfoDate
from thePages
CTE, for each page.像这样的东西(未经测试)。
如果您想要一个查询中的所有页面,您可以使用它。
Something like this (untested).
If you want all the pages in one query you could use this instead.