Question : sql query help

This query in my SP:

SELECT  @lat1= latitude,
        @long1 = longitude
FROM PostalCodes
WHERE  PostalCode = @ZipCode
SELECT PostalCode ,DistanceInMiles
FROM
(
 SELECT  PostalCode,3958.75 * ( Atan(Sqrt(1 - power(((Sin(@Lat1/57.2958) * Sin(latitude/57.2958)) +
    (Cos(@Lat1/57.2958) * Cos(latitude/57.2958) * Cos((longitude/57.2958) - (@Long1/57.2958)))), 2)) /
    ((Sin(@Lat1/57.2958) * Sin(latitude/57.2958)) + (Cos(@Lat1/57.2958) * Cos(latitude/57.2958) *
    Cos((longitude/57.2958) - (@Long1/57.2958)))))) DistanceInMiles
 FROM PostalCodes
) a
WHERE  a.DistanceInMiles > -1 and a.DistanceInMiles <= @GivenMileRadius and PostalCodes.Active=1
ORDER BY DistanceInMiles


im getting this error:  "Msg 4104, Level 16, State 1, Procedure up_FindPostalCodesWithinRadius, Line 19 The multi-part identifier "PostalCodes.Active" could not be bound."

If I take out the last part, it works, however active is a valid columnname in PostalCodes, and  I need only those zip codes where the column active = 1 to be included.  How to fix this query?

Answer : sql query help

resp:

SELECT  @lat1= latitude,
        @long1 = longitude
FROM PostalCodes
WHERE  PostalCode = @ZipCode
SELECT PostalCode ,DistanceInMiles
FROM
(
 SELECT  PostalCode, 3958.75 * ( Atan(Sqrt(1 - power(((Sin(@Lat1/57.2958) * Sin(latitude/57.2958)) +
    (Cos(@Lat1/57.2958) * Cos(latitude/57.2958) * Cos((longitude/57.2958) - (@Long1/57.2958)))), 2)) /
    ((Sin(@Lat1/57.2958) * Sin(latitude/57.2958)) + (Cos(@Lat1/57.2958) * Cos(latitude/57.2958) *
    Cos((longitude/57.2958) - (@Long1/57.2958)))))) DistanceInMiles
 FROM PostalCodes
  WHERE Active=1
) a
WHERE  a.DistanceInMiles > -1 and a.DistanceInMiles <= @GivenMileRadius
ORDER BY DistanceInMiles
Random Solutions  
 
programming4us programming4us