使用 order by 时如何添加列 [distanceInKm]?

How to add column [distanceInKm] when i use order by?

我有一个问题,因为我不知道如何添加 kolumn 当我在我的程序中使用 order by 排序客户和车辆之间的距离时 我的程序看起来像:

create procedure    [dbo].[p_top5_v2_type2222]
 @IdCustomer int,
 @idGroupVehicle int  as
 declare @start geography
  SET @start = (select location from Customer where idCustomer=@idCustomer )
select TOP 5 idVehicle,idGroupVehicle,brand,model,maxRange,weight,maxSpeed, nameLocation 
from Vehicle where idGroupVehicle= @idGroupVehicle  and  (@start.STDistance(locationVehicle)/1000 is not null)
order by @start.STDistance(locationVehicle)/1000 asc
GO

我的结果是

现在我想添加列 [distanceInKm] 看看它们是否真的分类好 我试试

create procedure    p_top5_v2_type_distance
 @IdCustomer int,
 @idGroupVehicle int  as
 declare @start geography
  SET @start = (select location from Customer where idCustomer=@idCustomer )
select TOP 5 idVehicle,idGroupVehicle,brand,model,maxRange,weight,maxSpeed, nameLocation 
from Vehicle where idGroupVehicle= @idGroupVehicle  and  (@start.STDistance(locationVehicle)/1000 [distanceInKm]  is not null)
order by @start.STDistance(locationVehicle)/1000 [distanceInKm] asc
GO

但我有错误

Msg 102, Level 15, State 1, Procedure p_top5_v2_type_distance, Line 7 [Batch Start Line 0]
Incorrect syntax near 'distanceInKm'.

谁能解释一下我如何添加列?

选择的列进入 select,而不是 order by。然后您可以在 ORDER BY:

中使用它
select TOP 5 idVehicle, idGroupVehicle, brand, model, maxRange,weight, maxSpeed, nameLocation ,
       @start.STDistance(locationVehicle)/1000 as distanceInKm
from Vehicle
where idGroupVehicle = @idGroupVehicle and  
      (@start.STDistance(locationVehicle)/1000 is not null)
order by distanceInKm asc