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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:04:07+00:00 2026-05-31T18:04:07+00:00

I am looking for the SQL syntax to use HAVING in the following statement:

  • 0

I am looking for the SQL syntax to use HAVING in the following statement:

DECLARE @ORIG_LAT AS FLOAT = 40.4882011413574; 
DECLARE @ORIG_LONG AS FLOAT = -80.1939010620117;
DECLARE @DISTANCE AS INT; 

SELECT LATITUDE_DEG, LONGITUDE_DEG,SQRT( 
    POWER(69.1 * (LATITUDE_DEG - @ORIG_LAT), 2) + 
    POWER(69.1 * (@ORIG_LONG - LONGITUDE_DEG) * COS(LATITUDE_DEG / 57.3), 2)) AS DISTANCE 
FROM NAVAIDS 
HAVING DISTANCE < 80 --error 
ORDER BY DISTANCE ASC; 

Error:

Msg 207, Level 16, State 1, Line 9
Invalid column name ‘distance’.

It’s ok with the ORDER BY but I don’t understand why it doesn’t like the HAVING. Any help with direction? It is SQL Server 2008 R2

  • 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-31T18:04:08+00:00Added an answer on May 31, 2026 at 6:04 pm

    As you have noticed, you can’t use an aliased column directly. The easiest solution would be to wrap your statement in a subselect and apply your clause on that.

    SELECT  *
    FROM    (
              SELECT  LATITUDE_DEG
                      , LONGITUDE_DEG
                      , SQRT(
                          POWER(69.1 * (LATITUDE_DEG - @ORIG_LAT), 2) +
                          POWER(69.1 * (@ORIG_LONG - LONGITUDE_DEG) * COS(LATITUDE_DEG / 57.3), 2)) AS DISTANCE
              FROM    NAVAIDS
            ) q          
    WHERE DISTANCE < 80
    ORDER BY 
            DISTANCE ASC; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking mainly at things like new SQL syntax, new kinds of locking, new
I'm looking for an SQL client app that can display subdatasheets like the following:
I've been looking at my screen too long today. I'm getting an SQL syntax
I have the following SQL script: USE MyDatabase ALTER DATABASE MyDatabase SET SINGLE_USER WITH
I am looking for SQL Server Profiler in SQL Server 2008 express edition.I have
I'm looking for SQL Server studio management-like and redgate sqlcompare/datacompare-like tools for MySQL. What
I'm looking to inspect SQL statements in Java/jdbc code to ensure that the SQL
I'm looking to implement SQL Server Reporting Services as our standard reporting platform in
i am looking for an SQL query, that selects exactly rows ordered by date
I'm preferably looking for a SQL query to accomplish this, but other options might

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.