views:

77

answers:

2

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.

+2  A: 

If you use an aggregate function, such as avg, you need to use partition by in your over clause, or include the columns selected that aren't aggregates in your group by clause.

Like so:

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

GROUP BY

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,
tbrm_CountryList.CountryImage,
tbrm_CategoryList.CategoryName,
tbrm_CategoryList.CategoryImage,
aspnet_Users.UserName


)  Article
WHERE Article.RowRank > @PageIndex AND RowRank <= (@PageIndex + @PageSize)
ORDER BY Article.ArticleDateTimeAdded DESC

If you wanted it just by CountryName, for example, you would drop the group by clause and use:

avg(tbrm_Votes.True) over (partition by tbrm_CountryList.CountryName) as Truth
Eric
Hi. I tried the group clause first and this ended giving me a timeout. I changed to the second suggestion of adding the partition clause and although it gives me the correct answer it is now returning duplicate rows for each article id in the votes table.I added:AVG(tbrm_Votes.True) OVER(partition by tbrm_Article.ArticleID) AS Truth,AVG(tbrm_Votes.False) OVER(partition by tbrm_Article.ArticleID) AS FalseAny ideas?
Cunners
Yeah, the `group by` will eliminate duplicate rows, but the `over` does not. You can do a `select distinct` with the `over (partition by..` and see if that performs any better for you.
Eric
A: 

Cunners,

I think it will help if you explain what you want, especially with the paging part of the picture. If you just add a PARTITION BY clause to the AVG() expressions' OVER clauses, you'll still get a row for every single vote, and I doubt that's what you want.

If I had to guess, I'd guess you want something like this, but I don't know the cardinalities of your joins (or much else), so it's just a guess.

WITH ARTICLE AS (
  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,
  Truth,
  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 
  CROSS APPLY (
    SELECT tbrm_Votes.ArticleID, AVG(tbrm_Votes.True), AVG(tbrm_Votes.False)
    FROM tbrm_Votes
    WHERE tbrm_Article.ArticleID = Votes.ArticleID
    GROUP BY tbrm_Votes.ArticleID
  ) AS Votes(ArticleID,Truth,False)
)
  SELECT * FROM ARTICLE
  WHERE ARTICLE.RowRank > @PageIndex AND ARTICLE.RowRank <= (@PageIndex + @PageSize)
  ORDER BY ARTICLE.ArticleDateTimeAdded DESC
Steve Kass
Hi Steve. Thanks for your help on this. To answer your earlier question about what I am trying to achieve I think you guessed correctly. I basically have an article object which users vote 1 to 10 on truth and also a 1 to 10 on false (i made these up so they probably dont make much sense). anyway each set of votes is stored in the votes table against each article id. i want to return an avg for the truth and also an avg for the false column against an article id. I get what you are trying to show me but i get an error I can't get past it is....
Cunners
'Article' is not a recognized option.Incorrect syntax near the keyword 'ON'.any help would be appreciated.
Cunners
See if the change I made helps. I'd botched the use of CROSS APPLY. Also, be sure you don't have a preceding statement that doesn't end with a semicolon.
Steve Kass
I don't see "Article" (not all upper case) in my example. Where is the error about Article coming from?
Steve Kass