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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:04:47+00:00 2026-05-27T15:04:47+00:00

Can I use a column calculated in a SQL Server view later on in

  • 0

Can I use a column calculated in a SQL Server view later on in that same view?

Let’s say I have the following view:

Select 
    t1.StartMile, t2.EndMile,  t2.EndMile- t1.StartMile as TotMile
from 
   TableStarts as t1 
inner join 
   TableEnds as t2 on t1.Id = t2.Id  

Is there a way to edit the view to do the following

Select  
    t1.StartMile, t2.EndMile, t2.EndMile - t1.StartMile as TotMile, 
    TotMile + 30  as EvenMoreMiles

I tried this and got error:

Invalid column name ‘TotMile’

Please don’t tell me to use t2.EndMile - t1.StartMile + 30 as EvenMoreMiles. TotMiles is a long case statement in my actual code.

I rather not have to create an intermediate view.

I am using SQL Server 2005.

ADDED LATER

Thanks for all the answers. I will upvote all.

The answers raise the following new question:

Given that there are thousands of rows, and TotMiles looks like the following, which of the answers given would be the most efficient? Or would it be most efficient to create an intermediary view?

CASE WHEN t .TaskType = 1  and  t .StartTime < '1/1/2012'  
               THEN (tv.EndMile - tv.StartMile )  
     WHEN NOT (t .Location1_PKey = c.pkey OR t .Location2_PKey = c.pkey)
              then (tv.EndMile - tv.StartMile )
     WHEN (tv.EndMile - tv.StartMile ) < 31  Then 0 
     ELSE  (tv.EndMile - tv.StartMile - 30 )
END AS MilesAdjusted2012,
  • 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-27T15:04:47+00:00Added an answer on May 27, 2026 at 3:04 pm

    You can also use CROSS APPLY which can be more concise, particularly if you are building up chains of aliases that reference preceding ones.

    SELECT t1.StartMile,
           t2.EndMile,
           TotMile,
           EvenMoreMiles,
           AndYetMoreMiles
    FROM   TableStarts AS t1
           INNER JOIN TableEnds AS t2
             ON t1.Id = t2.Id
           CROSS APPLY (SELECT t2.EndMile - t1.StartMile) A(TotMile)
           CROSS APPLY (SELECT TotMile + 30) A2(EvenMoreMiles)
           CROSS APPLY (SELECT EvenMoreMiles + 100) A3(AndYetMoreMiles)  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I use a calculated column multiple times in the same select without
I have the following SQL (SQL Server 2005) that I use to calculate the
We've the following SQL that we use to calculate total costs. SELECT DailyProduction.CustomerId as
I recently built a query in SQL that I can use to look at
I have a simple 3 column csv file that i need to use python
I have 3 home PCs that have 2008 SQL Server Express instances installed on
I can use VS08's MFC/ActiveX template to create a C++ ActiveX object that I
I have a datagridview bound from a datatable. I have a calculated column linetotal
I have the SQL query: select title, scale / next_scale, c from ( select
The following PL/SQL block works: DECLARE r TABLE1%ROWTYPE; BEGIN SELECT * INTO r FROM

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.