當我運行下面的SQL 08查詢時,出現tbrm_Article.ArticleID無效的錯誤,因爲它不包含在聚合函數或GROUP BY子句中。有人可以幫我糾正這個SQL 08查詢嗎?
我知道這個問題是關係到: AVG(tbrm_Votes.True)當成真理, AVG(tbrm_Votes.False)爲假, 和隨後加入到tbrm_Votes表。
tbrm_votes表包含文章ID,真實數字列,假數字列。我正在嘗試爲給定的文章ID平均所有的真實和虛假的數字。文章ID可以在投票表中多次出現。
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) AS Truth,
AVG(tbrm_Votes.False) AS False,
ROW_NUMBER() OVER (ORDER BY tbrm_Article.ArticleDateTimeAdded DESC) AS RowRank
FROM
tbrm_Article INNER JOIN
tbrm_CountryList ON tbrm_Article.CountryID = tbrm_CountryList.CountryID INNER JOIN
tbrm_CategoryList ON tbrm_Article.CategoryID = tbrm_CategoryList.CategoryID INNER JOIN
aspnet_Users ON tbrm_Article.UserID = aspnet_Users.UserID INNER JOIN
tbrm_Votes ON tbrm_Article.ArticleID = tbrm_Votes.ArticleID
) Article
WHERE Article.RowRank > @PageIndex AND RowRank <= (@PageIndex + @PageSize)
ORDER BY Article.ArticleDateTimeAdded DESC
如果任何人能告訴我我要去哪裏錯我會很感激。由於
不會OVER了還需要一個分區子句? – Lazarus 2009-09-22 11:34:26
沒有。 – Quassnoi 2009-09-22 11:40:17