SELECT nombre,descripcion,idcupon,idestadocupon,idclf,imagen AS cupon FROM cupon LEFT JOIN (SELECT idcliente,MIN(ABS(ABS(latitud)-ABS({ lat}))+ABS(ABS(longitud)-ABS({ LONG}))) AS distancia FROM clientesede GROUP BY idcliente) sede ON cupon.idcliente=sede.idcliente WHERE idestadocupon IN (2,5) AND idconsumidor LIKE { ncnd} AND idcupon LIKE { nrocp} AND descripcion LIKE { dscrip} ORDER BY sede.distancia