views:

27

answers:

2

I have following sql query when i execute it zero rows are displayed i have feild called distance with value null in it , when i execute this query the distance feild will be filled with some numerical value but if i specify condition distance<2 or distance <10 or ... same result gets displayed can i know the whats problem.

  SELECT Id,Name1,ZipCode,StreetName,StreetNumber,State1,Lat,
   Lng,Keyword, 
  ( 6371 * ACOS( COS( (12.925432/57.2958) ) 
   * COS(  (Lat/57.2958)  ) * COS( ( Lng/57.2958 ) - (77.5940171/57.2958) ) 
   + SIN( 12.925432/57.2958 ) * SIN(  Lat/57.2958  ) ) ) AS distance 
  FROM Business_Details 
  where ( 
   (StreetName like '%jayanagar%')
   and (Keyword like '%plumbing%' )
   and (distance<3)
   ) 
   ORDER BY distance;
+2  A: 

You got answers earlier...

[http://stackoverflow.com/questions/3608150/how-to-use-having-and-order-by-clause-in-sql]

You can not refer to field distance in where clause, you have to use:

( 6371 * ACOS( COS( (12.925432/57.2958) ) 
* COS(  (Lat/57.2958)  ) * COS( ( Lng/57.2958 ) - (77.5940171/57.2958) ) 
+ SIN( 12.925432/57.2958 ) * SIN(  Lat/57.2958  ) ) ) < 3
Michael Pakhantsov
Thanks for the reply ,i have made changes to my query as you suggested but the result is empty i am not able to track where exactly is the problem is their any alternate way to tackle this.
mahesh
A: 

This particular query can be sucessfully executed by creating view in sql . create view business as SELECT Id,Name1,ZipCode,StreetName,StreetNumber,State1,Lat,Lng,Keyword, ( 6371 * ACOS( COS( (12.925432/57.2958) ) * COS( (Lat/57.2958) ) * COS( ( Lng/57.2958 ) - (77.5940171/57.2958) ) + SIN( 12.925432/57.2958 ) * SIN( Lat/57.2958 ) ) ) AS distance FROM Business_Details ORDER BY distance; This particular view contains the results with distance feild then we can query the view business to manipulate the results for our distance value entered select * from business where (distance<2);

mahesh