sql SFMC-SQL地理distance.sql

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql SFMC-SQL地理distance.sql相关的知识,希望对你有一定的参考价值。

SELECT
   x.emailAddress
 , x.firstName
 , x.zip
 , x.store_zip
 , x.store_phone
 , x.ranking
 , x.distance
from (
    SELECT
      emailAddress
    , firstName
    , zip
    , store_zip
    , store_phone
    , row_number() over (
            partition by emailAddress
            order by emailAddress, distance
        ) as ranking /* list stores by distance */
    , distance
    FROM (
        SELECT
          customer.emailAddress
        , customer.firstName
        , customer.zip
        , store.store_zip
        , store.store_phone
        , ROUND(3961 * ACOS(
            CASE
            WHEN (pc1.XAxis * pc2.XAxis + pc1.YAxis *
                pc2.YAxis + pc1.ZAxis * pc2.ZAxis) > 1 THEN 1
            WHEN (pc1.XAxis * pc2.XAxis + pc1.YAxis *
                pc2.YAxis + pc1.ZAxis * pc2.ZAxis) < -1 THEN -1
            ELSE (pc1.XAxis * pc2.XAxis + pc1.YAxis *
                pc2.YAxis + pc1.ZAxis * pc2.ZAxis)
            END),0) distance
        FROM [store_customers] customer
        INNER JOIN [zip_axis] pc1 ON (pc1.zip = customer.zip)
        INNER JOIN [store_locations] store
            INNER JOIN [zip_axis] pc2 ON (pc2.zip = store.store_zip)
            ON ROUND(3961 * ACOS(
                CASE
                WHEN (pc1.XAxis * pc2.XAxis + pc1.YAxis *
                    pc2.YAxis + pc1.ZAxis * pc2.ZAxis) > 1 THEN 1
                WHEN (pc1.XAxis * pc2.XAxis + pc1.YAxis *
                    pc2.YAxis + pc1.ZAxis * pc2.ZAxis) < -1 THEN -1
                ELSE (pc1.XAxis * pc2.XAxis + pc1.YAxis *
                    pc2.YAxis + pc1.ZAxis * pc2.ZAxis)
                END),0
              ) > 0  /* set desired distance range here */
        WHERE customer.zip is not null
    ) rankedRows
) x
where x.ranking = 1

以上是关于sql SFMC-SQL地理distance.sql的主要内容,如果未能解决你的问题,请参考以下文章

sql SFMC-SQL创建,间二dates.sql

sql SFMC-SQL条次从 - dates.sql

sql SFMC-SQL排名,method1.sql

sql SFMC-SQL排名,method2.sql

sql SFMC-SQL分割随机-25-percent.sql

sql SFMC-SQL系分割随机-20-percent.sql