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).
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
Pagesadditionally 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 lastRateIDandInfoDatefor 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
RateIDandInfoDatefrom thePagesCTE, for each page.