I am having a few issues with the below SQL.
SELECT *
FROM (SELECT tbrm_Article.ArticleID,
tbrm_Article.CountryID,
tbrm_Article.CategoryID,
tbrm_Article.Title,
tbrm_Article.ArticleDetail,
tbrm_Article.Source,
tbrm_Article.ArticleDateTimeAdded,
tbrm_Article.ViewCount,
tbrm_Article.CommentCount,
tbrm_CountryList.CountryName AS CountryName,
tbrm_CountryList.CountryImage AS CountryImage,
tbrm_CategoryList.CategoryName AS CategoryName,
tbrm_CategoryList.CategoryImage AS CategoryImage,
aspnet_Users.UserName AS UserName,
AVG(tbrm_Votes.True) OVER() AS Truth,
AVG(tbrm_Votes.False) OVER() AS False,
ROW_NUMBER() OVER (ORDER BY tbrm_Article.ArticleDateTimeAdded DESC) AS RowRank
FROM tbrm_Article
JOIN tbrm_CountryList ON tbrm_Article.CountryID = tbrm_CountryList.CountryID
JOIN tbrm_CategoryList ON tbrm_Article.CategoryID = tbrm_CategoryList.CategoryID
JOIN aspnet_Users ON tbrm_Article.UserID = aspnet_Users.UserID
JOIN tbrm_Votes ON tbrm_Article.ArticleID = tbrm_Votes.ArticleID) Article
WHERE Article.RowRank > @PageIndex AND RowRank <= (@PageIndex + @PageSize)
ORDER BY Article.ArticleDateTimeAdded DESC
Everything works fine apart from the two AVG statements. Instead of averaging only one applicable relevant article id it is returning the average for the whole votes table of values. Any ideas of the best way to fix this? I am using SQL Server 08.