Current context: Paging query results
i need to know if there is some way to get calculated value in my query like row_number, that calculates the next new value over a specific ammount of rows.
if i use row_number it will generate a kind on identity column in my result, whet i whant is to ger it over a specified ammout of rows. like this:
Number of rows per page: 3
RowNum, PageNum, Value
1, 1, rowa
2, 1, rowb
3, 1, rowc
4, 2, rowd
5, 2, rowe
6, 2, rowf
7, 3, rowg
Use a ranking function , with a <partition_by_clause>
, which:
Divides the result set produced by the FROM clause into partitions to which the ROW_NUMBER function is applied. For the syntax of PARTITION BY, see OVER Clause.
For instance ROW_NUMBER() OVER(PARTITION BY ... ORDER BY ...)
:
WITH CTE
AS
(
SELECT *,
ROW_NUMBER() OVER(PARTITION BY PageNum
ORDER BY Id) AS Rownumber
FROM tablename
)
...
This should work:
WITH CTE AS
(
SELECT
RowNum = ROW_NUMBER()OVER (ORDER BY Value ASC),
Value
FROM
dbo.TableName
)
SELECT
RowNum,
PageNum = ((RowNum - 1) / 3) + 1,
Value
FROM
CTE
ORDER BY
RowNum ASC
Of course the 3
should be a parameter.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.