分页表另一个Sql问题

发布于 2024-12-03 23:33:56 字数 1216 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

坐在坟头思考人生 2024-12-10 23:33:56

您可以尝试执行以下操作:

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),以及每个页面的第一个和最后一个 RateIDInfoDate 。这是有效的,因为:

  • 每个页面的第一行的行号比页面大小的倍数高 1,例如 1、201、401、601 等 - 因此整数除以页面大小的余数将是始终为 1

  • 每页的最后一行都有一个可被页面大小整除的行号(例如 200、400、600 等),因此整数除法的余数为0

根据此信息,我可以从每个页面的 Pages CTE 中挑选出第一个和最后一个 RateIDInfoDate

You could try to do something like this:

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

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 last RateID and InfoDate 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 and InfoDate from the Pages CTE, for each page.

青瓷清茶倾城歌 2024-12-10 23:33:56

像这样的东西(未经测试)。

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

Something like this (untested).

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

If you want all the pages in one query you could use this instead.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文