简体   繁体   中英

WHERE clause on calculated field not working

I have an Access SQL query pulling back results from a Latitude & Longitude input (similar to a store locator). It works perfectly fine until I attempt to put in a WHERE clause limiting the results to only resultants within XXX miles (3 in my case).

The following query works fine without the WHERE distCalc < 3 clause being added in:

PARAMETERS
  [selNum] Long
, [selCBSA] Long
, [cosRadSelLAT] IEEEDouble
, [radSelLONG] IEEEDouble
, [sinRadSelLAT] IEEEDouble;
SELECT B.* FROM (
  SELECT A.* FROM (
    SELECT
      CERT
    , RSSDHCR
    , NAMEFULL
    , BRNUM
    , NAMEBR
    , ADDRESBR
    , CITYBR
    , STALPBR
    , ZIPBR
    , simsLAT
    , simsLONG
    , DEPDOM
    , DEPSUMBR
    , 3959 * ArcCOS(
        cosRadSelLAT
      * cosRadSimsLAT
      * cos(radSimsLONG - radSelLONG)
      + sinRadSelLAT
      * sinRadSimsLAT
      ) AS distCalc  
    FROM aBRc
    WHERE CBSA = selCBSA
    AND cosRadSimsLAT IS NOT NULL
    AND UNINUMBR <> selNum 
  ) AS A
  ORDER BY distCalc
) AS B
WHERE B.distCalc < 3 
ORDER BY B.DEPSUMBR DESC;

When I add the WHERE distCalc < 3 clause, I get the dreaded

This expression is typed incorrectly, or it is too complex to be evaluated.

error.

Given that the value is created in the A sub-query I thought that it would be available in the outer B query for comparative calcs. I could recalculate the distCalc in the WHERE , however, I'm trying to avoid that since I'm using a custom function ( ArcCOS ). I'm already doing one hit on each row and there is significant overhead involved doing additional if I can avoid it.

The way you have it typed you are limiting it by B.distCalc, which is requires calcuation of A.distCalc, on which you are asking for a sort. Even if this worked worked, would require n^2 calculations to compute.

Try putting the filter on distCalc in the inner query (using the formula for distCalc, not distCalc itself).

This is not an answer. It's a formatted comment. What happens when you do this:

select *
from (
select somefield, count(*) records
from sometable
group by somefield) temp

If that runs successfully, try it with

where records > 0

at the end. If that fails, you probably need another approach. If it succeeds, start building your real query using baby steps like this. Test early and test often.

I was able to "solve" the problem by pushing the complicated formula into the function and then returning the value (which was then able to be used in the WHERE clause).

Instead of:

    3959 * ArcCOS( cosRadSelLAT * cosRadSimsLAT * cos(radSimsLONG - radSelLONG) + sinRadSelLAT * sinRadSimsLAT) AS distCalc  

I went with:

    ArcCOS2(cosRadSelLAT,cosRadSimsLAT,radSimsLONG, radSelLONG,sinRadSelLAT,sinRadSimsLAT) AS distCalc

The ArcCOS2 Function contained the full formula. The upside is it works, the downside is that appears to be a slight tad slower. I appreciate everyone's help on this. Thank you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM