0

我写了一个函数来计算给定最大半径的两点之间的距离。该函数有效,但需要调用两次,我实现它的方式:

一次在 select 块中,然后再次在 where 块中。

有没有更好的方法来实现这一点?

感谢您的帮助!

select
    c.LABEL_NAME,
    k.*,
    dbo.kiw_GetNearMeetingLocationInMiles_FUNC(41.056466,-85.3312009,LATITUDE,LONGITUDE) as distance
    from USR_KIW_CUS_MEETING as k
    join CUSTOMER as c
    on c.MASTER_CUSTOMER_ID = k.MASTER_CUSTOMER_ID
    where c.USR_MEMBERSHIP_STATUS_CODE NOT IN('CR','CSD')
    and dbo.kiw_GetNearMeetingLocationInMiles_FUNC(41.056466,-85.3312009,LATITUDE,LONGITUDE)  <= 120;

更新

这是有效的,基于 Mathieu Rodic 的建议

SELECT
    *
FROM
(
    SELECT
        c.LABEL_NAME,
        k.*,
        dbo.kiw_GetNearMeetingLocationInMiles_FUNC(41.056466,-85.3312009,LATITUDE,LONGITUDE) as distance
    FROM
        USR_KIW_CUS_MEETING as k
    JOIN
        CUSTOMER as c on c.MASTER_CUSTOMER_ID = k.MASTER_CUSTOMER_ID
    WHERE
        c.USR_MEMBERSHIP_STATUS_CODE NOT IN('CR','CSD')
)a
WHERE
   a.distance <= 120
4

1 回答 1

1

尝试以下操作:

SELECT
    *
FROM
(
    SELECT
        c.LABEL_NAME,
        k.*,
        dbo.kiw_GetNearMeetingLocationInMiles_FUNC(41.056466,-85.3312009,LATITUDE,LONGITUDE) as distance
    FROM
        USR_KIW_CUS_MEETING as k
    JOIN
        CUSTOMER as c on c.MASTER_CUSTOMER_ID = k.MASTER_CUSTOMER_ID
    WHERE
        c.USR_MEMBERSHIP_STATUS_CODE NOT IN('CR','CSD')
) AS computed_table
WHERE
    distance <= 120

HAVING子句允许按计算列进行过滤。

于 2013-06-27T00:36:27.067 回答