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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:23:25+00:00 2026-05-16T06:23:25+00:00

I have two T-SQL scalar functions that both perform calculations over large sums of

  • 0

I have two T-SQL scalar functions that both perform calculations over large sums of data (taking ‘a lot’ of time) and return a value, e.g. CalculateAllIncomes(EmployeeID) and CalculateAllExpenditures(EmployeeID).

I run a select statement that calls these and returns results for each Employee. I also need the balance of each employee calculated as AllIncomes-AllExpenditures.

I have a function GetBalance(EmployeeID) that calls the two above mentioned functions and returns the result {CalculateAllIncomes(EmployeeID) - CalculateAllExpenditures(EmployeeID)}. But if I do:

Select CalculateAllIncomes(EmployeeID), CalculateAllExpenditures(EmployeeID), GetBalance(EmployeeID) .... the functions CalcualteAllIncomes() and CalculateAllExpenditures get called twice (once explicitly and once inside the GetBalance funcion) and so the resulting query takes twice as long as it should.

I’d like to find some better solution. I tried:

select alculateAllIncomes(EmployeeID), AS Incomes, CalculateAllExpenditures
(EmployeeID) AS Expenditures, (Incomes - Expenditures) AS Balance....

but it throws errors:

Invalid column name Incomes and

Invalid column name Expenditures.

I’m sure there has to be a simple solution, but I cannot figure it out. For some reason it seems that I am not able to use column Aliases in the SELECT clause. Is it so? And if so, what could be the workaround in this case?
Thanks for any suggestions.

  • 1 1 Answer
  • 1 View
  • 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-16T06:23:26+00:00Added an answer on May 16, 2026 at 6:23 am

    Forget function calls: you can probably do it everything in one normal query.

    Function calls misused (trying for OO encapsulation) force you into this situation. In addition, if you have GetBalance(EmployeeID) per row in the Employee table then you are CURSORing over the table. And you’ve now compounded this by multiple calls too.

    What you need is something like this:

    ;WITH cSUMs AS
    (
    SELECT
        SUM(CASE WHEN type = 'Incomes' THEN SomeValue ELSE 0 END) AS Income),
        SUM(CASE WHEN type = 'Expenditures' THEN SomeValue ELSE 0 END) AS Expenditure)
    FROM
        MyTable
    WHERE
        EmployeeID = @empID --optional for all employees
    GROUP BY
        EmployeeID 
    )
    SELECT
        Income, Expenditure, Income - Expenditure
    FROM
        cSUMs 
    

    I once got a query down from a weekend to under a second by eliminating this kind of OO thinking from a bog standard set based aggregate query.

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

Sidebar

Related Questions

Hi everyone I'm having a problem. I have two SQL queries that are both
I have two SQL tables with data that I would like to compare. The
I have two sql tables for user login, store and retrieve data according to
I have two SQL Server (2000) databases. Both are used for the same project,
I have two SQL 2005 instances that reside on different networks. I need to
I have Two SQL Query Both Return select round(convert(float,'24367.723'),2) Result:24367.72 Second: select convert(varchar(20),round(convert(float,'24367.723'),2)) Result:24367.7
I have two SQL Server databases A and B They both contain a table
I have two SQL Server 2005 instances that are geographically separated. Important databases are
I have two SQL queries that I'm running from a C# winform, and I
Suppose I have two SQL tables: Customers and PhoneNumbers. Suppose that Customers has 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.