# Complex SQL query

**URL:** <https://community.dreamfactory.com/t/complex-sql-query/2191>\
**Category:** REST API\
**Created:** [February 14, 2016, 9:21pm UTC](https://community.dreamfactory.com/t/complex-sql-query/2191 "2016-02-14T21:21:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![garreeoke](https://avatars.discourse-cdn.com/v4/letter/g/eb9ed0/32.png) [@garreeoke](https://community.dreamfactory.com/u/garreeoke)\
**Post date:** [February 14, 2016, 9:21pm UTC](https://community.dreamfactory.com/t/complex-sql-query/2191/1 "2016-02-14T21:21:57Z")

</div>

How would I make the following query a rest query in to dreamfactory?

SELECT id, name,( 3959 \* acos( cos( radians(33) ) \* cos( radians( 33 ) ) \* cos( radians( -117 ) - radians(-117) ) + sin( radians(33) ) \* sin( radians( 33 ) ) ) ) AS distance FROM wu\_object HAVING distance \< 10 ORDER BY distance LIMIT 0 , 10;

---

<div class="post-metadata">

**Author:** ![juniorconte](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/juniorconte/32/1007_2.png) [@juniorconte](https://community.dreamfactory.com/u/juniorconte)\
**Post date:** [February 16, 2016, 1:01am UTC](https://community.dreamfactory.com/t/complex-sql-query/2191/2 "2016-02-16T01:01:15Z")

</div>

Hi @garreeoke, I suggest you do a stored procedure.

[https://wiki.dreamfactory.com/DreamFactory/Tutorials/Querying\_stored\_procedures](https://wiki.dreamfactory.com/DreamFactory/Tutorials/Querying_stored_procedures)

best regards,

---

<div class="post-metadata">

**Author:** ![abhijitmehta](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/abhijitmehta/32/439_2.png) [@abhijitmehta](https://community.dreamfactory.com/u/abhijitmehta)\
**Post date:** [February 16, 2016, 11:31am UTC](https://community.dreamfactory.com/t/complex-sql-query/2191/3 "2016-02-16T11:31:18Z")

</div>

Hi ,

Were you able to achieve this . I require exactly the same thing done i.e. get all nearby locations from a table storing the lattitudes and longitudes , based on input lattitude and longitude.

In general , can you build a select queryon a table that takes as input a bind variable ( or literal) and expose that as REST using DF ?

---

<div class="post-metadata">

**Author:** ![abhijitmehta](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/abhijitmehta/32/439_2.png) [@abhijitmehta](https://community.dreamfactory.com/u/abhijitmehta)\
**Post date:** [February 16, 2016, 12:28pm UTC](https://community.dreamfactory.com/t/complex-sql-query/2191/4 "2016-02-16T12:28:53Z")

</div>

Okay , I used the stored procedure method. Works very well for me . Let me know if you want any details on the implementation.  
Cheers .
