Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7826139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:16:39+00:00 2026-06-02T09:16:39+00:00

So I’m working on SQL Server 2008 and I have this query which should

  • 0

So I’m working on SQL Server 2008 and I have this query which should be quite simple, but for some reason doesn’t work. It basically looks like that:

SELECT TOP 10
    u.Id                AS "UserId",
    u.CreationDate      AS "Member since",
    AVG(q.Score)        AS "Average Question Rating",  
    COUNT(q.Id)         AS "N. of Questions posted by the agent",
    AVG(a.Score)        AS "Average Answer Rating",  
    COUNT(a.Id)         AS "N. of Answers posted by the agent"
FROM    
        Users u, 
        Answers a, 
        Questions q
WHERE q.OwnerUserId = u.Id
AND a.OwnerUserId = u.Id
GROUP BY u.Id, u.CreationDate

When I only work on either the Answers table or the Questions table, everything is ok. But as soon as I try to do both at once (like in the query above), the COUNTs don’t work at all. What I get is that the COUNT(a.Id) is identical to the COUNT(q.Id). So I tried reducing my query to see what was wrong, and I realized that I just had to add the Questions or the Answers table (even without using them anywhere) to the FROM clause when working with the other table and everything was ruined.

I’m sure it’s something ridiculously trivial that I have overlooked but it’s driving me crazy, I’d be thankful if anybody could point me what went wrong. Thank you in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T09:16:40+00:00Added an answer on June 2, 2026 at 9:16 am

    You’re not joining Answers and Questions correctly for the aggregation. Between Answers and Questions, the result is a cartesian product (for every user, every answer is coupled with every question)

    The simplest way to correct this is to perform aggregation in subqueries:

    SELECT TOP 10
        u.Id                AS "UserId",
        u.CreationDate      AS "Member since",
        ISNULL((SELECT AVG(Score) FROM Answers   WHERE OwnerUserId = u.Id), 0)
                            AS "Average Question Rating",  
               (SELECT COUNT(*)   FROM Answers   WHERE OwnerUserId = u.Id)        
                            AS "N. of Questions posted by the agent",
        ISNULL((SELECT AVG(Score) FROM Questions WHERE OwnerUserId = u.Id), 0)
                            AS "Average Answer Rating",  
               (SELECT COUNT(*)   FROM Questions WHERE OwnerUserId = u.Id)
                            AS "N. of Answers posted by the agent"
    FROM  Users u
    

    Alternatively using joins:

    SELECT TOP 10
         u.Id                AS "UserId",
         u.CreationDate      AS "Member since",
         ISNULL(q.a, 0)      AS "Average Question Rating",  
         ISNULL(q.c, 0)      AS "N. of Questions posted by the agent",
         ISNULL(a.a, 0)      AS "Average Answer Rating",  
         ISNULL(a.c, 0)      AS "N. of Answers posted by the agent"
    FROM Users u
    -- If you LEFT JOIN these tables, you'll get also results for users without
    -- questions or answers
    LEFT OUTER JOIN (SELECT OwnerUserId, AVG(Score) a, COUNT(*) c 
         FROM Questions GROUP BY OwnerUserId) q
         ON  q.OwnerUserId = u.Id
    LEFT OUTER JOIN (SELECT OwnerUserId, AVG(Score) a, COUNT(*) c 
         FROM Answers GROUP BY OwnerUserId) a
         ON  a.OwnerUserId = u.Id
    

    I don’t know SQL Server’s query optimiser well enough, so I can’t say which one is going to be faster. The first solution could take advantage of scalar subquery caching, if that is available in SQL Server. Otherwise, the second query maybe performs less nested loops.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.