sql statement

Try to run below sql, and alert about the Distance is not valid column.
Why and how to fix it?


SELECT  City , BusinessName,geoCodeLoc1,geoCodeLoc2,Zip,
      
      ( 3960 * acos( cos( radians( @custLat ) ) *
      cos( radians( GeoCodeLoc1 ) ) * cos( radians(  GeoCodeLoc2  ) - radians( @custLng ) ) +
      sin( radians( @custLat ) ) * sin( radians(  GeoCodeLoc1  ) ) ) ) AS Distance
FROM Agency
WHERE Distance <= @distanceInMile
solution1368Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SharathData EngineerCommented:
Can you post complete code or explain more with some sample on what exactly you are looking for.
0
rshqCommented:
Hi
 Are you sure Distance  is a field of table Agency?
0
jogosCommented:
SELECT  City , BusinessName,geoCodeLoc1,geoCodeLoc2,Zip,
      
      ( 3960 * acos( cos( radians( @custLat ) ) *
      cos( radians( GeoCodeLoc1 ) ) * cos( radians(  GeoCodeLoc2  ) - radians( @custLng ) ) +
      sin( radians( @custLat ) ) * sin( radians(  GeoCodeLoc1  ) ) ) ) AS Distance
FROM Agency
WHERE  ( 3960 * acos( cos( radians( @custLat ) ) *
      cos( radians( GeoCodeLoc1 ) ) * cos( radians(  GeoCodeLoc2  ) - radians( @custLng ) ) +
      sin( radians( @custLat ) ) * sin( radians(  GeoCodeLoc1  ) ) ) ) <= @distanceInMile

Open in new window

OR
SELECT x.* FROM 
(
SELECT  City , BusinessName,geoCodeLoc1,geoCodeLoc2,Zip,
      
      ( 3960 * acos( cos( radians( @custLat ) ) *
      cos( radians( GeoCodeLoc1 ) ) * cos( radians(  GeoCodeLoc2  ) - radians( @custLng ) ) +
      sin( radians( @custLat ) ) * sin( radians(  GeoCodeLoc1  ) ) ) ) AS Distance
FROM Agency) as x
WHERE x.Distance <= @distanceInMile

Open in new window

0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Brian CroweDatabase AdministratorCommented:
OR

WITH cteAgency (City, BusinessName, geoCodeLoc1, geoCodeLoc2, Zip, Distance)
AS
(
   SELECT  City , BusinessName,geoCodeLoc1,geoCodeLoc2,Zip,
     
      ( 3960 * acos( cos( radians( @custLat ) ) *
      cos( radians( GeoCodeLoc1 ) ) * cos( radians(  GeoCodeLoc2  ) - radians( @custLng ) ) +
      sin( radians( @custLat ) ) * sin( radians(  GeoCodeLoc1  ) ) ) ) AS Distance
   FROM Agency
)
SELECT *
FROM cteAgency
WHERE Distance <= @distanceInMile
0
PortletPaulfreelancerCommented:
To help explain why:
  it is not possible* in SQL to use that "column alias" in the WHERE clause,
  but you can use a column alias established in a subquery in an outer query:
SELECT
        City
      , BusinessName
      , geoCodeLoc1
      , geoCodeLoc2
      , Zip
      , (
        3960 * ACOS(COS(RADIANS(@custLat))
        * COS(RADIANS(GeoCodeLoc1)) * COS(RADIANS(GeoCodeLoc2) - RADIANS(@custLng))
        + SIN(RADIANS(@custLat)) * SIN(RADIANS(GeoCodeLoc1)))
        ) AS Distance -- << a "column alias"
FROM Agency
WHERE Distance <= @distanceInMile -- cannot use this "column alias" in this where clause
;
SELECT
        City
      , BusinessName
      , geoCodeLoc1
      , geoCodeLoc2
      , Zip
      , Distance
FROM (
        SELECT
                City
              , BusinessName
              , geoCodeLoc1
              , geoCodeLoc2
              , Zip
              , (
                3960 * ACOS(COS(RADIANS(@custLat))
                * COS(RADIANS(GeoCodeLoc1)) * COS(RADIANS(GeoCodeLoc2) - RADIANS(@custLng))
                + SIN(RADIANS(@custLat)) * SIN(RADIANS(GeoCodeLoc1)))
                ) AS Distance -- << a "column alias"
        FROM Agency
      ) AS Derived
WHERE Distance <= @distanceInMile -- >> but can use "column alias" of a nested subquery
;

Open in new window

*The only exception to this known to me is if using Teradata but that isn't relevant here.

Note, while using the "with as ( subquery )"** is a valid option, it's not necessarily the best option, and a simple nested subquery is sufficient for this need.

** called a "Common Table Expression" or CTE. These things are great! but best used in my opinion when you need recursion or to avoid repeating a subquery (re-use).

no criticism implied for putting it up as an option - because it is a valid option.

Lastly, although you cannot use column aliases in the where clause, it is valid syntax to use them in the ORDER BY.
0
Scott PletcherSenior DBACommented:
All these are valid options and will work.

But I strongly prefer to use CROSS APPLY for this, mainly because they can be easily nested without forcing you to hide the original table under layers of CTEs or subqueries, and which, if not coded carefully, can often cause performance issues:


SELECT  City, BusinessName, geoCodeLoc1, geoCodeLoc2, Zip,
      ca1.Distance
FROM dbo.Agency
CROSS APPLY (
      SELECT ( 3960 * acos( cos( radians( @custLat ) ) *
      cos( radians( GeoCodeLoc1 ) ) * cos( radians(  GeoCodeLoc2  ) - radians( @custLng ) ) +
      sin( radians( @custLat ) ) * sin( radians(  GeoCodeLoc1  ) ) ) ) AS Distance
) AS ca1 --ca1 = "CROSS APPLY 1"
/*
CROSS APPLY (
      SELECT Distance --note that you can use any/all previous CROSS APPLY "aliases"
          / SomeOtherColumn AS SomeNewAlias
AS sample_ca2
...
*/
WHERE Distance <= @distanceInMile
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.