I am able to run a query that returns n rows, within a specified date range (one row for each date). I want to be able to recall this same query and get either the "next" n rows within that same specified date range, or the "previous" n rows.
The rows need to be presented ASC by Date.
This is the query atm:
<snip>
CREATE PROCEDURE dbo.sp_BalancesAsAtAggs
@dateFrom datetime,
@dateTo datetime,
@dateEdge datetime,
@direction varchar(10),
@numRows int
AS
IF (@direction = 'Next')
BEGIN
SET ROWCOUNT @numRows
SELECT (Some select stuff that works and aggregates data based on date - returns one row for each date)
FROM tblBalancesAsAt
WHERE convert(datetime, left(AsAtDate, 11)) >= convert(varchar, left(@dateFrom,11))
and convert(datetime, left(AsAtDate, 11)) <=convert(varchar, left(@dateTo,11))
and convert(datetime, left(AsAtDate, 11)) < convert(varchar, left(@dateEdge,11))
GROUP BY AsAtDate
ORDER BY AsAtDate ASC
END
ELSE
IF (@direction= 'Previous')
BEGIN
SELECT *
FROM ( SET ROWCOUNT @numRows
SELECT (As above)
FROM tblBalancesAsAt
WHERE convert(datetime, left(AsAtDate, 11)) >= convert(varchar, left(@dateFrom,11))
and convert(datetime, left(AsAtDate, 11)) <=convert(varchar, left(@dateTo,11))
and convert(datetime, left(AsAtDate, 11)) < convert(varchar, left(@dateEdge,11))
GROUP BY AsAtDate
ORDER BY AsAtDate DESC
)
ORDER BY AsAtDate DESC
END
GO
</snip>
Currently, I have "Next" working. The PROBLEM is:
I can't seem to use "SET ROWCOUNT" within the SELECT statement. I have tried executing the inner query separately - ie. if I run it separately it works, but I can't exec it from within the FROM clause (ie. effectively nesting).
Any suggestions?
The rows need to be presented ASC by Date.
This is the query atm:
<snip>
CREATE PROCEDURE dbo.sp_BalancesAsAtAggs
@dateFrom datetime,
@dateTo datetime,
@dateEdge datetime,
@direction varchar(10),
@numRows int
AS
IF (@direction = 'Next')
BEGIN
SET ROWCOUNT @numRows
SELECT (Some select stuff that works and aggregates data based on date - returns one row for each date)
FROM tblBalancesAsAt
WHERE convert(datetime, left(AsAtDate, 11)) >= convert(varchar, left(@dateFrom,11))
and convert(datetime, left(AsAtDate, 11)) <=convert(varchar, left(@dateTo,11))
and convert(datetime, left(AsAtDate, 11)) < convert(varchar, left(@dateEdge,11))
GROUP BY AsAtDate
ORDER BY AsAtDate ASC
END
ELSE
IF (@direction= 'Previous')
BEGIN
SELECT *
FROM ( SET ROWCOUNT @numRows
SELECT (As above)
FROM tblBalancesAsAt
WHERE convert(datetime, left(AsAtDate, 11)) >= convert(varchar, left(@dateFrom,11))
and convert(datetime, left(AsAtDate, 11)) <=convert(varchar, left(@dateTo,11))
and convert(datetime, left(AsAtDate, 11)) < convert(varchar, left(@dateEdge,11))
GROUP BY AsAtDate
ORDER BY AsAtDate DESC
)
ORDER BY AsAtDate DESC
END
GO
</snip>
Currently, I have "Next" working. The PROBLEM is:
I can't seem to use "SET ROWCOUNT" within the SELECT statement. I have tried executing the inner query separately - ie. if I run it separately it works, but I can't exec it from within the FROM clause (ie. effectively nesting).
Any suggestions?