views:

71

answers:

2

I'm trying to get all locations stored in the database with an sql statement described here

My sql statement is:

SELECT TOP(10)
                *,
                (
                    6371 *
                    acos(
                        cos(
                            radians(<cfqueryparam cfsqltype="CF_SQL_Numeric" value="#FORM.latitude#">)
                        ) *
                        cos(
                            radians( custLat )
                        ) *
                        cos(
                            radians( custLong ) -
                            radians(<cfqueryparam cfsqltype="CF_SQL_Numeric" value="#FORM.longtitude#">)
                        ) +
                        sin(
                            radians(<cfqueryparam cfsqltype="CF_SQL_Numeric" value="#FORM.latitude#">)
                        ) *
                        sin(
                            radians( custLat )
                        )
                    )
                ) AS distance
                FROM customers
                HAVING distance < 25
                ORDER BY distance;

Problem is I get an error and can't figure out why... error: Invalid column name 'distance'.

on this line: radians(<cfqueryparam cfsqltype="CF_SQL_Numeric" value="#FORM.latitude#">) (the second occurrence)

Why do I get this error and how do I fix it?

+2  A: 

What you did it wrong. Here is solution :

SELECT distance FROM

(SELECT TOP(10)

                *,
                (
                    6371 *
                    acos(
                        cos(
                            radians(<cfqueryparam cfsqltype="CF_SQL_Numeric" value="#FORM.latitude#">)
                        ) *
                        cos(
                            radians( custLat )
                        ) *
                        cos(
                            radians( custLong ) -
                            radians(<cfqueryparam cfsqltype="CF_SQL_Numeric value="#FORM.longtitude#">)
                        ) +
                        sin(
                            radians(<cfqueryparam cfsqltype="CF_SQL_Numeric"  value="#FORM.latitude#">)
                        ) *
                        sin(
                            radians( custLat )
                        )
                    )
                ) AS distance
                FROM customers
                ) newTable
                HAVING distance < 25
                GROUP BY distance
                ORDER BY distance;
ppshein
this gives a different error:Column 'newTable.distance' is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause.
red-X
Sorry. Need to put GROUP BY clause between HAVING and ORDER BY.
ppshein
@ppshein you should really edit your answer to contain the correct code, then.
Adam Tuttle
Yes you should, especially since that worked, thnx.
red-X
Sorry. I've updated it.
ppshein
A: 

You can solve it using creating a stored procedure or view in sql server or mysql server.

mahesh