SQL 使用 select 语句添加构建 table - 无插入操作

SQL use select statement to add build a table - no insert op

我需要用select语句组成table

DECLARE @MetresPerMile FLOAT = 1609.344;
DECLARE @LOCStart1 GEOGRAPHY = GEOGRAPHY::Point(48.83000,-97.31000,4326)
DECLARE @LOCDest1 GEOGRAPHY = GEOGRAPHY::Point(48.83000,-97.31000,4326)
DECLARE @LOCStart2 GEOGRAPHY = GEOGRAPHY::Point(22.9230000,-94.5342000,4326)
DECLARE @LOCDest2 GEOGRAPHY = GEOGRAPHY::Point(22.9230000,-94.5342000,4326)

SELECT 
    '1' [Start], '1' [Dest], 
    @LOCStart1.STDistance(@LOCDest1) / @MetresPerMile [Distance],
    '2' [Start], '2' [Dest],
    @LOCStart2.STDistance(@LOCDest1) / @MetresPerMile [Distance];

我得到的结果是 6 列,我只需要 3 列:

这会给你结果:

declare @MetresPerMile float = 1609.344;
declare @Points table (Id integer, Point geography);

insert into @Points (Id, Point) 
       values (1, GEOGRAPHY::Point(48.83000,-97.31000,4326)),
              (2, GEOGRAPHY::Point(22.9230000,-94.5342000,4326));

select Start.Id as Start, Dest.Id as Dest, 
       Start.Point.STDistance(Dest.Point)/@MetresPerMile
from @Points as Start
     cross join @Points as Dest

您可以在点 table 中添加任意数量的点,select 将 return 所有点之间的距离。

你可以看到它在这里工作:Fiddle

感谢 Marc Guillot 的回答,这是否得到了您想要的结果?

declare @MetresPerMile float = 1609.344;

with Points as (
  select *
  from (values
    (1, GEOGRAPHY::Point(48.83000,-97.31000,4326)),
    (2, GEOGRAPHY::Point(22.9230000,-94.5342000,4326))
  ) pts (Id, Point)
)
select Start.Id as Start, Dest.Id as Dest, 
       Start.Point.STDistance(Dest.Point)/@MetresPerMile as Distance
from Points as Start
cross join Points as Dest