Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

database - How to perform a row-by-row geographic computation using a scalar function in SQL

First off, I apologize if the question appears confusing. To elaborate, I have a scalar function that calculates the bearing in degrees between two geographic points. I wanted to test this function on two tables, both consisting of US cities and their respective latitude/longitude coordinates before I use it on much larger datasets. It accepts two parameters: an origin and a destination variable which are of type geography. The problem I currently have is that the function only calculates the last row of the two test tables. Please see the code below:

-- Create the test tables with columns
DECLARE @TableA TABLE (CITY VARCHAR(25), LAT NUMERIC(19,15), LON NUMERIC(19,15))
DECLARE @TableB TABLE (CITY VARCHAR(25), LAT NUMERIC(19,15), LON NUMERIC(19,15))

-- Insert cities, lat/lon coordinates 
INSERT INTO @TableA (CITY, LAT, LON)
VALUES ('Kansas City, MO', 39.099724, -94.578331), 
       ('Memphis, TN', 35.117500, -89.971107),
       ('Austin, TX', 30.266666, -97.733330),
       ('Palm Springs, CA', 33.830517, -116.545601)

INSERT INTO @TableB (CITY, LAT, LON)
VALUES ('Aurora, CO', 39.710835, -104.812500),
       ('Indianapolis, IN', 39.791000, -86.148003),
       ('Vancouver, WA', 45.633331, -122.599998),
       ('Joliet, IL',   41.520557, -88.150558)

-- Declare variables
DECLARE @origin AS GEOGRAPHY
DECLARE @dest AS GEOGRAPHY

-- Convert to geography 
-- SELECT was used instead of SET since I thought SELECT allows for more than one column value
SELECT @origin = GEOGRAPHY::STPointFromText('POINT('+CONVERT(VARCHAR(50), LON)+' '+CONVERT(VARCHAR(50), LAT)+')', 4326) FROM @TableA
SELECT @dest = GEOGRAPHY::STPointFromText('POINT('+CONVERT(VARCHAR(50), LON)+' '+CONVERT(VARCHAR(50), LAT)+')', 4326) FROM @TableB

-- Print variables to 'Results'
SELECT @origin.ToString()
SELECT @dest.ToString()

-- Call function
SELECT dbo.fnBearing(@origin, @dest) AS RESULT

The two SELECT statements for @origin.ToString() and @dest.ToString() only display lat/lon coordinates for Palm Springs, CA and Joliet, IL. When I call the function, it also only returns the calculation for these two geographic points. I would like for this function to perform a row-by-row calculation between Tables A and B (i.e. Kansas City, MO is calculated against Aurora, CO; Memphis, TN is calculated against Indianapolis, IN, etc.). I understand CROSS APPLY can be used for such a case, though I am unsure how to write that out in this situation.

Is there a specific way to set the two variables equal to each subsequent geographic column value so that when I pass them in as parameters to the function it will return a column with four calculations?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...