I am trying to implement paging using Row_Number() method in this query but with no luck.
The following Query uses recursive way to get Sites for a set of categories.
WITH hierarchy AS (
SELECT yt.id
FROM [dbo].[TH_Categories] yt
WHERE yt.ID = @topicID And CultureID = @cultureID
UNION ALL
SELECT yt.id
FROM [dbo].[TH_Categories] yt
JOIN hierarchy h ON h.ID = yt.ParentCategoryID)
Select id, [SiteName]
,[SiteURL]
,[Description]
,[Logo]
,[CultureID]
,[DateAdded], dbo.GetSiteFollowers(id) AS Followers from dbo.TH_Sites where id in (
Select Distinct SiteID from dbo.TH_CategoryFeeds Where CatID in (
SELECT ID
FROM hierarchy t ))
This query returns all the sites for a set of categories. I am trying to integrate Row_Number() to the dbo.TH_Sites table so I can get x number of records for each request using the
Where RowNumber BETWEEN @rowStart AND @rowEnd
But I keep getting T-SQL errors.
Any tips guys, thanks.
I am trying this :
WITH hierarchy AS (
SELECT yt.id
FROM [dbo].[TH_Categories] yt
WHERE yt.ID = @topicID And CultureID = @cultureID
UNION ALL
SELECT yt.id
FROM [dbo].[TH_Categories] yt
JOIN hierarchy h ON h.ID = yt.ParentCategoryID)
Select id, [SiteName]
,[SiteURL]
,[Description]
,[Logo]
,[CultureID]
,[DateAdded], ROW_NUMBER() OVER (order by [DateAdded] DESC) AS 'RowNumber' , dbo.GetSiteFollowers(id) AS Followers from dbo.TH_Sites where RowNumber = 5 AND id in (
Select Distinct SiteID from dbo.TH_CategoryFeeds Where CatID in (
SELECT ID
FROM hierarchy t))
But I am getting RowNumber is not a valid column
Here is another Implementation :
WITH hierarchy AS (
SELECT yt.id
FROM [dbo].[TH_Categories] yt
WHERE yt.ID = @topicID And CultureID = @cultureID UNION ALL SELECT yt.id
FROM [dbo].[TH_Categories] yt
JOIN hierarchy h ON h.ID = yt.ParentCategoryID)
WITH numbered_hierarchy AS (
Select id, [SiteName]
,[SiteURL]
,[Description]
,[Logo]
,[CultureID]
,[DateAdded] , ROW_NUMBER() OVER (order by [DateAdded] DESC) AS 'RowNumber', dbo.GetSiteFollowers(id) AS Followers from dbo.TH_Sites where id in (
Select Distinct SiteID from dbo.TH_CategoryFeeds Where CatID in ( SELECT ID FROM hierarchy t )) )
SELECT id
, [SiteName]
, [SiteURL]
, [Description]
, [Logo]
, [CultureID]
, [DateAdded]
, RowNumber
, Followers
FROM numbered_hierarchy
WHERE RowNumber BETWEEN 1 AND 5