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

  • Home
  • SEARCH
  • 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 6048193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:26:09+00:00 2026-05-23T07:26:09+00:00

The problem I am having seems to come from my where clause and hardcoding.

  • 0

The problem I am having seems to come from my where clause and hardcoding.
I have two tables one with ID’s and another with ID’s and Scores for the ID’s.

So my code looks a little something like this:

SELECT AVG(CAST(c.Score AS DEC(10,2))) AS avgTestC,
       AVG(CAST(d.Score AS DEC(10,2))) AS avgTestD,
       AVG(CAST(e.Score AS DEC(10,2))) AS avgTestE,
       AVG(CAST(f.Score AS DEC(10,2))) AS avgTestF,
       COUNT(DISTINCT c.ID) AS CountC,
       COUNT(DISTINCT d.ID) AS CountD,
       COUNT(DISTINCT e.ID) AS CountE,
       COUNT(DISTINCT f.ID) AS CountF
FROM tblWithIds a,
     JOIN tblScores b ON a.ID = b.ID AND b.Year = @Year
     LEFT JOIN tblScores c ON a.ID = c.ID AND c.Year = @Year
     LEFT JOIN tblScores d ON a.ID = d.ID AND d.Year = @Year
     LEFT JOIN tblScores e ON a.ID = e.ID AND e.Year = @Year
     LEFT JOIN tblScores f ON a.ID = f.ID AND f.Year = @Year
WHERE c.TestC = 'Test C'
      d.TestD = 'Test D'
      e.TestE = 'Test E'
      f.TestF = 'Test F'

Now the problem is that when I add more to the where clause and almost identical “Tests” the where clause, it gives me NULL values for everything and 0’s for the COUNT. The code above works properly but the test am I using have very similar names and the table was made poorly so all of the “Tests” names’ are all in one column. I cannot debug as I am stuck using MSSQL 05 for now. Please help!

EDIT: After getting using the aggregate function SUM below from EricZ, if anyone else is interested I have found that

AVG( CASE WHEN b.Test = 'Test D' THEN CAST(b.Score AS DEC(10,2)) ELSE NULL END) AS avgTestC

to be the correct form for the AVG aggregate function.

  • 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-05-23T07:26:10+00:00Added an answer on May 23, 2026 at 7:26 am

    You can use CASE to do COUNT, and just need JOIN table once

    SELECT 
           AVG(CAST((CASE WHEN b.TestC = 'Test C' THEN b.Score ELSE 0 END) AS DEC(10,2))) AS avgTestC,
           AVG(CAST((CASE WHEN b.TestD = 'Test D' THEN b.Score ELSE 0 END) AS DEC(10,2))) AS avgTestD,
           AVG(CAST((CASE WHEN b.TestE = 'Test E' THEN b.Score ELSE 0 END) AS DEC(10,2))) AS avgTestE,
           AVG(CAST((CASE WHEN b.TestF = 'Test F' THEN b.Score ELSE 0 END) AS DEC(10,2))) AS avgTestF,             
           SUM(CASE WHEN b.TestC = 'Test C' THEN 1 ELSE 0 END) AS CountC,
           SUM(CASE WHEN b.TestD = 'Test D' THEN 1 ELSE 0 END)  AS CountD,
           SUM(CASE WHEN b.TestE = 'Test E' THEN 1 ELSE 0 END)  AS CountE,
           SUM(CASE WHEN b.TestF = 'Test F' THEN 1 ELSE 0 END)  AS CountF
    FROM tblWithIds a,
    JOIN tblScores b ON a.ID = b.ID AND b.Year = @Year
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a problem with my Seam code and I can't seem to figure
We were having a problem with our build server not checking out modifications from
Having a problem getting a TreeView control to display node images. The code below
I have a Java program which is being started via ProcessBuilder from another Java
I'm having some trouble deciding how two classes should interact and the problem I'm
We're having problem with a huge number of legacy stored procedures at work. Do
I'm having problem in the following line: rd.PrintOptions.PaperSize = PaperSize.PaperFanfoldStdGerman; it throws an exception
I'm wondering what the best practice is for handling the problem with having to
Im having a problem with a final part of my assignment. We get in
I'm having a problem with an application hanging and giving me the default Please

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.