是否可以使用SQL语句实现以下逻辑?如果实施,在30分钟的时间范围内处理4000个订单时,是否存在任何潜在的性能风险。我们有SQL Server作为我们的数据库。
我有2张表格如下。
订单
CUST_ID Cust_WHID EMP_ID纬度经度
C10001 WH142001 ?????? 13.051738 77.629877
C10002 WH142001 ?????? 13.047688 77.629279
C10003 WH142001 ?????? 13.043722 77.623373
C10004 WH142002 ?????? 13.047213 77.628853
User_Validation
USER_ID User_WHID纬度经度Current_Capacity
EMP100001 WH100001 13.033304 77.636592 5
EMP100002 WH100001 13.032645 77.629908 3
EMP100003 WH100001 13.046191 77.625797 0
EMP100004 WH100002 13.044981 77.626154 2
EMP100005 WH100002 13.052305 77.631151 4
EMP100006 WH100002 13.051004 77.630786 0
现在我想使用以下标准更新Orders表中的Emp_ID字段。
我尝试了以下逻辑,我知道当有多个记录时它不会起作用。我甚至尝试了一条记录,但在声明中出现了错误。
Update Orders Set Emp_ID=User_Validation.User_ID from (Select User_ID from
User_Validation where Current_Capacity <='6' and (SELECT Top 1 User_ID, (( 6367450 *
acos( cos( radians(Orders.Latitude) ) * cos( radians( User_Validation.Latitude ) ) *
cos( radians( User_Validation.Longitude ) - radians(Orders.Longitude) ) + sin(
radians(Orders.Latitude) ) * sin( radians( User_Validation.Latitude ) ) ) )) AS
distance_Mtrs FROM Orders, User_Validation order by distance_Mtrs)) where
Orders.Cust_WHID=User_Validation.User_WHID
答案 0 :(得分:0)
也许以下查询可能会指导您
with cte as (
SELECT
Cust_ID, Cust_WHID,
User_ID, User_WHID, Current_Capacity,
case when Current_Capacity >= 6 then 1 else 0 end as isfull,
dbo.fnGetDistance(o.Latitude, o.Longitude, e.Latitude, e.Longitude, 'Meters') distance
FROM Orders o, User_Validation e
-- ORDER BY Current_Capacity
), cte2 as (
select *, row_number() over (order by distance, isfull, Current_Capacity) rn
from cte
-- ORDER BY distance, isfull, Current_Capacity
)
select * from cte2
where rn = 1
--ORDER BY distance, isfull, Current_Capacity
我使用Jim Jackson编写的用户函数来计算距离(请参阅http://weblogs.asp.net/jimjackson/archive/2009/02/13/calculating-distances-between-latitude-and-longitude-t-sql-haversine.aspx)