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 6061009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:53:24+00:00 2026-05-23T08:53:24+00:00

I am trying to avoid using a scalar valued functions in my project, so

  • 0

I am trying to avoid using a scalar valued functions in my project, so I decided to try and convert one of them into a table valued function using a CTE.

I understand that the performance of scalar valued functions is poor because it they have to be executed for each row, and SQL server cannot optimise it in any way (i.e. it acts as a black box).

Here is my first attempt at converting it into a table valued function…

CREATE FUNCTION [dbo].[fn_get_job_average] (@jobnumber VARCHAR(50))
RETURNS TABLE AS RETURN
(   

  WITH JobAverage AS
  (
    SELECT job.Jobnumber, CAST(AVG(CAST(jobMark.Mark AS DECIMAL(18,1))) AS DECIMAL(18,1)) AS Average
    FROM  job 
      INNER JOIN jobMark 
        ON job.Guid = jobMark.Guid
    WHERE job.Jobnumber = @jobnumber
    GROUP BY job.Jobnumber
  )
  SELECT Jobnumber,
    CASE
      WHEN EXISTS(SELECT * FROM JobAverage) THEN Average
      ELSE 0.0 -- This never executes???, i.e. for job records that don't have a mark nothing is returned
    END AS Average
  FROM JobAverage
)

I want to output a table with the job number and average score.

For jobs that do have the mark, it appears to be OK. That is, the average is returned along with the jobnumer.

For jobs that do not have a mark, it seems to go wrong. The ELSE part of the statement does not execute. That is, I don’t get 0.0 returned as a the job average. No records are returned. Am I missing something?

Sorry I am not an experienced SQL developer, so I might have a few glaring mistakes in the above code. However, I am confused why a it doesn’t work.

Thanks 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-05-23T08:53:24+00:00Added an answer on May 23, 2026 at 8:53 am

    Not tested but something like this should do what you want.

       SELECT job.Jobnumber, COALESCE(CAST(AVG(CAST(jobMark.Mark AS DECIMAL(18,1))) AS DECIMAL(18,1)), 0.0) AS Average
        FROM  job 
          LEFT OUTER JOIN jobMark 
            ON job.Guid = jobMark.Guid
        WHERE job.Jobnumber = @jobnumber
        GROUP BY job.Jobnumber
    

    No need to use a CTE.

    BTW: What you do is that you check for Exists in the CTE Jobnumber in the case statement. If there are no rows in the CTE you will end up in the else part but since you use the CTE Jobnumber in the from clause of the main query you will not get any rows because the CTE Jobnumber did not return any rows.

    So to be perfectly clear of what is happening. The case statement will never be executed if there are no rows in the CTE Jobnumber.

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

Sidebar

Related Questions

I'm trying to avoid using Interface Builder as much as possible. At the moment
I'm trying to avoid a composite control or using and ASCX by extending an
After trying to avoid JavaScript for years, Iv started using Query for validation in
I'm trying to avoid using a long else if statement. Lets say I have
For readability reasons I'm trying to avoid using Char based case constructs, using Java
I'm working with Android at the moment, trying to avoid using Eclipse (for which
I'm trying to avoid using queries in while loops. Therefore I've come to the
I'm trying to avoid using an in memory database for testing (though I might
From the get go, let me say that I'm trying to avoid using pcntl_fork()
I'm trying to avoid using straight SQL queries in my web app. I looked

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.