I am setting up a system that finds providers in a specific region, calculates the average amount charged by them and brings this value.
But before that he will only pick up the providers that are available on the requested date. I have these 2 queries done and separated, but I'm not getting a way to put both in a single.
Availability rule query:
SELECT *
FROM fcs_prestadores_pedidos
WHERE hora_entrada not BETWEEN '2017-08-10 16:00:00' AND '2017-08-10 18:00:00'
AND hora_saida not between '2017-08-10 16:00:00' AND '2017-08-10 18:00:00'
Query with calculation of the average value within the 90km radius
SELECT avg(valor)
FROM (SELECT valor,(6371 * acos( cos( radians(-23.5427450) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-46.6513010) ) + sin( radians(-23.5427450) ) * sin( radians( lat ))))
AS distancia
FROM fcs_prestadores) virtual
WHERE distancia < 90
How can I first make the availability query, then calculate the average value?