Sqlserver
 sql >> Baza danych >  >> RDS >> Sqlserver

Cała długość łat. tabeli w zakresie sql w promieniu 15 km do każdej długości łat. innej tabeli kw. 2008

        /*
            Assuming Your tables are like so
        */
        IF OBJECT_ID('#xLocation1') IS NOT NULL
            DROP TABLE #xLocation1
        CREATE TABLE #xLocation1 (
             Id         INT IDENTITY(1,1) CONSTRAINT PK_Location_1 PRIMARY KEY--Reqire this for Geog Spatial Index
            ,LocationId INT
            ,Latitude   FLOAT   NULL
            ,Longitude  FLOAT   NULL
            ,Radius     INT     NULL
            ,GeogPoint  GEOGRAPHY   NULL
        )


        IF OBJECT_ID('#xLocation2') IS NOT NULL
            DROP TABLE #xLocation2
        CREATE TABLE #xLocation2 (
             Id         INT IDENTITY(1,1) CONSTRAINT PK_Location_2 PRIMARY KEY--Reqire this for Geog Spatial Index
            ,LocationId INT
            ,Latitude   FLOAT   NULL
            ,Longitude  FLOAT   NULL
            ,Radius     INT     NULL
            ,GeogPoint  GEOGRAPHY   NULL
        )


        DECLARE @Radius INT = 15 --KM

        /*
            Create GEOGRAPHY POINT datatypes
        */

        UPDATE #xLocation1
        SET
            GeogPoint   = GEOGRAPHY::STGeomFromText('POINT(' + CAST(ISNULL(Longitude,'') AS VARCHAR(20)) + ' ' + CAST(ISNULL(Latitude,'') AS VARCHAR(20)) + ')', 4326)

        UPDATE #xLocation2
        SET
            GeogPoint   = GEOGRAPHY::STGeomFromText('POINT(' + CAST(ISNULL(Longitude,'') AS VARCHAR(20)) + ' ' + CAST(ISNULL(Latitude,'') AS VARCHAR(20)) + ')', 4326)


        /*
            CREATE SPATIAL INDEXes
        */
        CREATE SPATIAL INDEX [SDX_Location1_GeogPoint_x1] ON #xLocation1 ( [GeogPoint] ) 
        USING GEOGRAPHY_GRID 
        WITH 
        (     GRIDS=(LEVEL_1 = HIGH,LEVEL_2 = HIGH,LEVEL_3 = HIGH,LEVEL_4 = HIGH) 
                , CELLS_PER_OBJECT = 64 
                , PAD_INDEX = OFF 
                , SORT_IN_TEMPDB = OFF 
                , DROP_EXISTING = OFF 
                , ALLOW_ROW_LOCKS = ON 
                , ALLOW_PAGE_LOCKS = ON 
        ) 

        CREATE SPATIAL INDEX [SDX_Location2_GeogPoint_x2] ON #xLocation2 ( [GeogPoint] ) 
        USING GEOGRAPHY_GRID 
        WITH 
        (     GRIDS=(LEVEL_1 = HIGH,LEVEL_2 = HIGH,LEVEL_3 = HIGH,LEVEL_4 = HIGH) 
                , CELLS_PER_OBJECT = 64 
                , PAD_INDEX = OFF 
                , SORT_IN_TEMPDB = OFF 
                , DROP_EXISTING = OFF 
                , ALLOW_ROW_LOCKS = ON 
                , ALLOW_PAGE_LOCKS = ON 
        ) 

        /*
            Find where locations from each table are within @Radius of each other
        */
        SELECT *
        FROM 
            #xLocation1 X
        INNER JOIN
            #xLocation2 P ON X.GeogPoint.STDistance(P.GeogPoint) <= @Radius



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Jak sprawdzić istnienie typu tabeli definiowanej przez użytkownika w SQL Server 2008?

  2. Co to jest LEN() w SQL Server?

  3. Zmodyfikuj ograniczenie CHECK w programie SQL Server za pomocą T-SQL

  4. Jak wstawić/pobrać pliki programu Excel do kolumny varbinary(max) w programie SQL Server 2008?

  5. 7 sposobów na zwrócenie wszystkich tabel z kluczami obcymi w SQL Server