如何做到这一点

时间:2019-07-26 06:38:09

标签: sql-server tsql stored-procedures

我有3个主表,例如:停车位置,装载位置,卸货位置,这些将提供标准分配的位置。

我有一张桌子,它提供从旅程的起点到终点的每分钟GPS位置。这个巨大的桌子包含了超过1000万个数据。比如说“旅行”表

输出:我需要查看每个GPS引脚,并为每个数据点分配最近的位置。

解决方案::我正在考虑从“旅行”表中读取数据并比较数据并返回最近位置的过程。

问题:如何将参数动态传递给Procedure参数。

@LATITUDE decimal(18,6),--->> should pick All distinct value form 'Travel" table
@LONGITUDE  decimal(18,6)--->> should pick  All distinct value form 'Travel" table


CREATE PROCEDURE [dbo].[CalculateDistance]
(
    @LATITUDE decimal(18,6),
    @LONGITUDE  decimal(18,6)

)
AS
begin

;with CTE AS (
SELECT      TOP 1
            'DischargeSite'                             AS 'NearestLocationType',
            CAST([Site_ID] AS VARCHAR)      AS 'NearestLocationID',
            [SiteName]                      AS 'NearestLocationName',
            [XCoordinate],
            [YCoordinate],
            [CountryCode],
            111.045 * DEGREES(ACOS(COS(RADIANS(@LATITUDE)) * COS(RADIANS([XCoordinate])) * COS(RADIANS(@LONGITUDE) - 
            RADIANS([YCoordinate])) + SIN(RADIANS(@LATITUDE)) * SIN(RADIANS([XCoordinate])))) AS 'DistanceToNearest'
FROM        [Discharge]     
WHERE       [XCoordinate] IS NOT NULL OR [YCoordinate] IS NOT NULL
ORDER BY    DistanceToNearest

UNION

SELECT      TOP 1
            'LoadingPlant'              AS 'NearestLocationType',
            [Plant_ID]      AS 'NearestLocationID',
            [PlantText]     AS 'NearestLocationName',
            [XCoordinate],
            [YCoordinate],
            [CountryCode],
            111.045 * DEGREES(ACOS(COS(RADIANS(@LATITUDE)) * COS(RADIANS([XCoordinate])) * COS(RADIANS(@LONGITUDE) - 
            RADIANS([YCoordinate])) + SIN(RADIANS(@LATITUDE)) * SIN(RADIANS([XCoordinate])))) AS 'DistanceToNearest'
FROM        [Loading]
WHERE       [XCoordinate] IS NOT NULL OR [YCoordinate] IS NOT NULL
ORDER BY    DistanceToNearest

UNION

SELECT      TOP 1
            'ParkingLocations'              AS 'NearestLocationType',
            CAST([Parking_ID] AS VARCHAR)   AS 'NearestLocationID',
            [ParkingText]       AS 'NearestLocationName',
            [XCoordinate],
            [YCoordinate],
            [CountryCode],
            111.045 * DEGREES(ACOS(COS(RADIANS(@LATITUDE)) * COS(RADIANS([XCoordinate])) * COS(RADIANS(@LONGITUDE) - 
            RADIANS([YCoordinate])) + SIN(RADIANS(@LATITUDE)) * SIN(RADIANS([XCoordinate])))) AS 'DistanceToNearest'
FROM        [Parking]
WHERE       [XCoordinate] IS NOT NULL OR [YCoordinate] IS NOT NULL
ORDER BY    DistanceToNearest
)
SELECT      TOP 1 *
FROM        CTE
ORDER BY    DistanceToNearest
END

0 个答案:

没有答案