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)。
答案 0 :(得分:2)
像这样(未经测试)。
SELECT @PageNumber as PageNo,
min(RateID) as StartIndex,
max(RateID) as EndIndex,
min(infoDate) as StartDate,
max(infoDate) as EndDate
FROM (
SELECT
(ROW_NUMBER()OVER (ORDER BY InfoDate ASC)) AS RowNo,
[RateID],
[InfoDate]
FROM Rate
) AS T
WHERE t.RowNo BETWEEN 200*@PageNumber AND 200 * (@PageNumber+1)-1
如果您希望一个查询中的所有页面都可以使用它。
SELECT PageNo + 1 as PageNo,
MIN(RateID) as StartIndex,
MAX(RateID) as EndIndex,
MIN(infoDate) as StartDate,
MAX(infoDate) as EndDate
FROM (
SELECT
(ROW_NUMBER() OVER(ORDER BY InfoDate ASC) - 1) / @PageSize AS PageNo,
[RateID],
[InfoDate]
FROM Rate
) AS T
GROUP BY PageNo
ORDER BY PageNo
答案 1 :(得分:2)
您可以尝试这样做:
DECLARE @PageSize INT = 200
;WITH PagingInfo AS
(
SELECT
RateID, InfoDate,
ROW_NUMBER() OVER (ORDER BY InfoDate) AS RowNo
FROM
dbo.Rate
),
Pages AS
(
SELECT
RateID,
InfoDate,
RowNo,
((RowNo - 1) / @PageSize) AS PageNo
FROM PagingInfo
)
SELECT
p.PageNo,
(SELECT RateID FROM Pages p2 WHERE p2.PageNo = p.PageNo AND p2.RowNo % @PageSize = 1) AS 'First RateID',
(SELECT RateID FROM Pages p2 WHERE p2.PageNo = p.PageNo AND p2.RowNo % @PageSize = 0) AS 'Last RateID',
(SELECT InfoDate FROM Pages p2 WHERE p2.PageNo = p.PageNo AND p2.RowNo % @PageSize = 1) AS 'First InfoDate',
(SELECT InfoDate FROM Pages p2 WHERE p2.PageNo = p.PageNo AND p2.RowNo % @PageSize = 0) AS 'Last InfoDate'
FROM Pages p
WHERE p.RowNo % @PageSize = 0
两个CTE基本上和你一样 - 它们为数据提供分页。第二个CTE Pages
还提供每行的页码。
从这些CTE中,我选择相关的 - 页码(来自Pages
),以及每页的第一个和最后一个RateID
和InfoDate
。这是因为:
每个页面的第一行的行号高于您的页面大小的倍数,例如1,201,401,601等 - 所以整数除以页面大小的余数将始终为1
每个页面的最后一行都有一个可以被页面大小整除的行号(例如200,400,600等),因此整数除法的余数为0
根据这些信息,我可以从每个页面的RateID
CTE中选出第一个和最后一个InfoDate
和Pages
。