How to achieve pagination in SQL Server 2008 with like statement - sql-server

My query for pagination result is
SELECT
*
FROM
(SELECT
ROW_NUMBER() OVER (ORDER BY Id DESC) AS RowNum,
*
FROM
My_Table) AS RowConstrainedResult
WHERE
(RowNum >= 1 AND RowNum <= 10)
ORDER BY
RowNum
This works fine. But when I try this for search result with like statement, it didn't work.
Search query
SELECT
*
FROM
(SELECT
ROW_NUMBER() OVER (ORDER BY Id DESC) AS RowNum,
*
FROM
My_Table) AS RowConstrainedResult
WHERE
column1 LIKE '%search%'
AND (RowNum >= 1 AND RowNum <= 10)
ORDER BY
RowNum
If first 10 rows don't contains search value, its return 0 results instead looking for full table. I can't use offset and fetch because it doesn't support in SQL Server 2008.
Any better way to use for pagination which work fine for like and normal in SQL Server 2008
Thanks

You're really close.. I'd use a CTE to filter, then select your 10 rows from it.
;WITH X AS
(
SELECT ROW_NUMBER() OVER (ORDER BY Id DESC) AS RowNum, *
FROM My_Table
WHERE column1 LIKE '%search%'
)
SELECT *
FROM X
WHERE RowNum BETWEEN 1 AND 10
ORDER BY RowNum
Depending on your situation, you may be able to simply get away with:
SELECT TOP 10 *, ROW_NUMBER() OVER (ORDER BY Id DESC) AS RowNum
FROM My_Table
WHERE column1 LIKE '%search%'
ORDER BY RowNum

Related

selecting the next set of top 30 records

This is my query:
SELECT Top 30 *
FROM (SELECT *, Row_number() OVER( PARTITION BY EntityPicURL
ORDER BY FavoriteCount desc) AS RN
FROM TweetEntity
WHERE HashTag LIKE '%%23RIPOlgaSyahputra%') A
WHERE RN = 1
ORDER BY FavoriteCount desc , LastModifieddateTime desc
This will select the first 30 unique records of the column entitypicURl. Now that I want to select the next 30 records (31-60).
This is a sort of a query i used earlier but this returned many duplicate entries of entitypicURL.
select *
from (select *, row_no = row_number() over (order by FavoriteCount desc,
LastModifiedDateTime desc)
from TweetEntity
where HashTag like '%%23RIPOlgaSyahputra%') e
where e.row_no > 30 and e.row_no <=60
Now i want to combine the value of first query and include e.row_no>30 and e.row_no<60 from the second query.
It is not a duplicate. My confusion is just combining two queries because both has got row_numbers.
You can use nested CTEs like this:
;WITH CTE1 AS (
SELECT *,
ROW_NUMBER() OVER(PARTITION BY EntityPicURL
ORDER BY FavoriteCount desc) AS RN
FROM TweetEntity
WHERE HashTag like '%%23RIPOlgaSyahputra%'
), CTE2 AS (
SELECT *,
ROW_NUMBER() OVER (ORDER FavoriteCount DESC,
LastModifiedDateTime DESC) AS row_no
FROM CTE1
WHERE RN = 1
)
SELECT *
FROM CTE2
WHERE row_no > 30 and row_no <=60
CTE2 will apply ROW_NUMBER on the filtered by RN = 1 result set of CTE1.
You can Use OFFSET to fetch records like this
you can select 30 records from below query
Select * FROM TweetEntity
ORDER BY FavoriteCount desc OFFSET 0 ROWS FETCH NEXT 30 ROWS ONLY
you can select next 30 records from below query
Select * FROM TweetEntity
ORDER BY FavoriteCount desc OFFSET 30 ROWS FETCH NEXT 30 ROWS ONLY
Note: OFFSET Will work on Sql server 2012+
Limitations in Using OFFSET-FETCH
ORDER BY is mandatory to use OFFSET and FETCH clause.
OFFSET clause is mandatory with FETCH. You can never use, ORDER BY …
FETCH.
TOP cannot be combined with OFFSET and FETCH in the same query
expression.
The OFFSET/FETCH rowcount expression can be any arithmetic, constant,
or parameter expression that will return an integer value. The
rowcount expression does not support scalar sub-queries.

SQL Server: How to select fist 10 records from table without using TOP keyword

There is a table which contains 50 records. I want to select first 10 records without using TOP keyword.
In SQL Server 2012+ you can use OFFSET ... FETCH
SELECT *
FROM YourTable
ORDER BY YourColumn ASC
OFFSET 0 ROWS
FETCH FIRST 10 ROWS ONLY
You can use ROW_NUMBER and Common Table Expression to query any range of data.
USE AdventureWorks2012;
GO
WITH OrderedOrders AS
(
SELECT SalesOrderID, OrderDate,
ROW_NUMBER() OVER (ORDER BY OrderDate) AS RowNumber
FROM Sales.SalesOrderHeader
)
SELECT SalesOrderID, OrderDate, RowNumber
FROM OrderedOrders
WHERE RowNumber <= 10 -- other conditions: RowNumber between 50 and 60
Refere ROW NUMBER Here
Although it's probably the same thing internally, you can use
set rowcount 10
and then run the query.
I guess you can try something like this:
SELECT t.Id, t.Name FROM Table t
WHERE 10 > (SELECT count(*) FROM Table t2 WHERE t.id > t2.id)
You can use ROW_NUMBER. Let's say your table contains columns ID and Name. In that case you can use such query:
SELECT t.Id, t.Name
FROM (
SELECT ID, Name,
ROW_NUMBER() OVER (ORDER BY Id) AS RowNumber
FROM TableName
) t
WHERE RowNumber <= 10

Windowed functions and NEXT VALUE FOR functions do not support constants as ORDER BY clause expressions

When running this statement I get the error Windowed functions and NEXT VALUE FOR functions do not support constants as ORDER BY clause expressions.
SELECT * FROM (select ROW_NUMBER() OVER (ORDER BY 'publishdate DESC') as RowNum,
* FROM news WHERE publishdate <=getdate()) as info
WHERE RowNum > 0 AND RowNum <= (100)
I want to use this statement to retrieve a resultset in a paged gridview.
How to get this statement to run?
SELECT * FROM (select ROW_NUMBER() OVER (ORDER BY publishdate DESC) as RowNum,
* FROM news WHERE publishdate <=getdate()) as info
WHERE RowNum > 0 AND RowNum <= (100)
You dont need the ' in (ORDER BY publishdate DESC)
For future reference, I've also seen this error when asking for a page of data that doesn't exist (asking for more rows than exist in the db).

Filter first then select page

How to first filter the result based on params then to apply where-between?
Some thing like
With Results as
(
Select colName,Title, Row_Number(Over...) as row from a table where colName=5
)
Select * from Results
where
row between #first and #last
But it does not works. I need to move my where colName=5 from with clause to outside then I got wrong data as It first get rows between #first n #last then search for colName=5.
Also I want count of Results.
Any idea?
You can use COUNT(*) OVER() to get the count of the unfiltered results
WITH cte as
(
select *,
ROW_NUMBER() over (order by name desc) AS RN,
count(*) over() AS [Count]
from master..spt_values
)
SELECT name, number,[Count]
FROM cte
WHERE RN BETWEEN 20 AND 24
Returns
name number Count
----------------------------------- ----------- -----------
VIEW 8278 2506
VIEW 8278 2506
view 2 2506
varchar 3 2506
varbinary 1 2506
This has performance implications though. You might want to just calculate the COUNT up front and cache it somewhere rather than recalculating it for every page request.
Your ROW_NUMBER syntax is incorrect. It should be this:
With Results as
(
SELECT colName, Title, ROW_NUMBER() OVER (ORDER BY ...) AS RN
FROM your_table
WHERE colName = 5
)
SELECT * FROM Results
WHERE rn BETWEEN #first AND #last
ORDER BY rn
See the documentation for more information.
I use approach very similar to Martin Smiths (currently selected answer) and at least in the tests I've made it gives better performance results.
; WITH cte as
(
select *,
ROW_NUMBER() over (order by name desc) AS RN
from master..spt_values
)
SELECT name, number, (SELECT COUNT(*) FROM cte) AS [Count]
FROM cte
WHERE RN BETWEEN 20 AND 24
Run this and his queries side by side and compare execution plans.

autonumber in select statement in SQL Server

I would like to create a select query statement with autonumber.. like..
select * from tbl1
will give me everything from table.
The result I'd like to get is..
1 data
2 data
3 data
So how can I do to get that number..??
like..
select (for autonumber), * from tbl1
the data in my table will repeated (no unique data)
Use ROW_NUMBER:
SELECT ROW_NUMBER() OVER (ORDER BY col1) AS rn, * FROM tbl1
To filter the results based on the row number use this:
SELECT * FROM
(
SELECT ROW_NUMBER() OVER (ORDER BY col1) AS rn, * FROM tbl1
) T1
WHERE rn = 5
You may need to find the identity's offset e.g. last ID of second table:
DECLARE #lastAutoID int
SET #lastAutoID = abs(( Select max(convert(float,[ConsID]))
FROM [RXPIPEDB]...[consumption] ) )
Then use ROW_NUMBER():
#lastAutoID + ROW_NUMBER() OVER (ORDER BY oldICN_str)

Resources