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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:07:07+00:00 2026-06-13T11:07:07+00:00

In a SQL view, what is the best way to handle the problem of

  • 0

In a SQL view, what is the best way to handle the problem of reusing previous calculations such that they do not become complex/unreadable.

In a stored proc, we could store/output @variables and do the calcs using them as we went along, but my problem is that this must be done in a view.

What’s the best way to go about this? (bearing in mind there are a few thousand rows of data).

SELECT  
        /* CALC 1 output to view row */
        (SELECT Act.ValueInContractCurrency/dbo.[Contract].Value * 
            (SELECT ISNULL(SUM(dbo.Invoice.ValueInContractCurrency),0)
             FROM dbo.Invoice 
             WHERE dbo.Invoice.StageId = Act.StageId)) AS InvoicedValueInContractCurrency, 

        /* CALC 2 wraps CALC1 inside it and outputs to view row */
        (SELECT Act.ValueInContractCurrency - 
            (SELECT Act.ValueInContractCurrency/dbo.[Contract].Value * 
                (SELECT ISNULL(SUM(dbo.Invoice.ValueInContractCurrency),0)
                 FROM dbo.Invoice 
                 WHERE dbo.Invoice.StageId = Act.StageId))) AS RemainingValueInContractCurrency,        

        /* CALC 3 wraps CALC2 inside it (which in turn wraps CALC1) and outputs to view row */
        (SELECT ConCurrency.CurrentExchangeRate / dbo.[Contract].CostedExchangeRate *
            (SELECT Act.ValueInContractCurrency - 
                (SELECT Act.ValueInContractCurrency/dbo.[Contract].Value * 
                    (SELECT ISNULL(SUM(dbo.Invoice.ValueInContractCurrency),0)
                     FROM dbo.Invoice 
                     WHERE dbo.Invoice.StageId = Act.StageId)))) AS RemainingValueInFacilityCurrency

        /* etc... for 10 more calcs that get increasingly long and unreadable via wrapping */

        FROM dbo.Activity AS Act
        JOIN dbo.Stage ON Act.StageId = dbo.Stage.Id       
        JOIN dbo.[Contract] ON dbo.Stage.ContractId = dbo.[Contract].Id
        JOIN dbo.Facility  ON dbo.[Contract].FacilityId = Facility.Id
        JOIN dbo.Currency AS FacCurrency ON dbo.Facility.CurrencyId = FacCurrency.Id
        JOIN dbo.Currency AS ConCurrency ON dbo.[Contract].CurrencyId = ConCurrency.Id
  • 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-13T11:07:09+00:00Added an answer on June 13, 2026 at 11:07 am

    Personally, I’d just subquery them. Although to be honest, SQL Server sees through the “wraps” and does reuse the expressions; you can verify that by checking the execution plan.

            /* etc... for 10 more calcs that get increasingly long and unreadable via wrapping */
    
    SELECT  *,
            /* CALC 3 wraps CALC2 inside it (which in turn wraps CALC1) and outputs to view row */
            (SELECT CurrentExchangeRate / CostedExchangeRate *
                RemainingValueInContractCurrency) AS RemainingValueInFacilityCurrency
    FROM (
    SELECT  *,
            /* CALC 2 wraps CALC1 inside it and outputs to view row */
            (SELECT ValueInContractCurrency - 
                InvoicedValueInContractCurrency) AS RemainingValueInContractCurrency
    FROM (
    SELECT  *,  
            /* CALC 1 output to view row */
            (SELECT Act.ValueInContractCurrency/dbo.[Contract].Value * 
                (SELECT ISNULL(SUM(dbo.Invoice.ValueInContractCurrency),0)
                 FROM dbo.Invoice 
                 WHERE dbo.Invoice.StageId = Act.StageId)) AS InvoicedValueInContractCurrency
            FROM dbo.Activity AS Act
            JOIN dbo.Stage ON Act.StageId = dbo.Stage.Id       
            JOIN dbo.[Contract] ON dbo.Stage.ContractId = dbo.[Contract].Id
            JOIN dbo.Facility  ON dbo.[Contract].FacilityId = Facility.Id
            JOIN dbo.Currency AS FacCurrency ON dbo.Facility.CurrencyId = FacCurrency.Id
            JOIN dbo.Currency AS ConCurrency ON dbo.[Contract].CurrencyId = ConCurrency.Id
            ) A
            ) B
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MS SQL view that I want to make available as a
How can I optimize below SQL (simplified view of my complex query.) Ideally, I
This question already has an answer here: view sql that linq-to-sql produces 3 Answers
I have a view in SQL Server 2008 that I want to use for
Is there a straight forward way to view the SQL command text actually executed
I'm looking for the best way to handle columns which are calculated on the
I read this already: The Best Way to shred XML data into SQL Server
Does anybody know what is the best approach to accessing a sql view through
What is the best way to encrypt a SQL 2005 database? I have found
I think this is a simple SQL query problem, but I am just not

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.