0

我想为我的客户找到最近的员工。

我有 2 张桌子

Customer_Master
Customer_ID  Customer_Name   Cust_Location
Cust100001   Subash          0xE6100000010C1B2E724F57172A408449F1F109685340
Cust100002   Naresh          0xE6100000010CBE30992A18152A4093AAED26F8675340

Employee_Master
Emp_ID      Emp_name  Emp_Location
Emp100001   Prakash   0xE6100000010C363B527DE7172A4069C36169E0675340
Emp100002   Suresh    0xE6100000010C98C3EE3B86172A4064E597C118685340
Emp100003   Vincent   0xE6100000010CE5B8533A58172A4090DD054A0A685340
Emp100004   Paul      0xE6100000010C2EE6E786A6142A40A0A696ADF5675340

我有客户和员工的位置。现在我想选择一个离客户最近的员工。

我尝试了以下显示错误的查询

Select Emp_ID 
from Emp_Master 
where Emp_ID.Emp_Location.STDistance
(
    Select Cust_Location 
    from Cust_Master 
    where Cust_ID='Cust100001'
)
4

2 回答 2

1

也许这会有所帮助:

USE Test;
GO

CREATE TABLE Customers
(
    ID INT NOT NULL CONSTRAINT PK_Customers PRIMARY KEY CLUSTERED IDENTITY(1,1)
    , CustomerName NVARCHAR(255)
    , LOC GEOMETRY
);

CREATE TABLE Employees
(
    ID INT NOT NULL CONSTRAINT PK_Employees PRIMARY KEY CLUSTERED IDENTITY(1,1)
    , EmployeeName NVARCHAR(255)
    , LOC GEOMETRY
);

TRUNCATE TABLE Customers;
TRUNCATE TABLE Employees;

INSERT INTO Customers (CustomerName, LOC) VALUES ('Customer1',0xE6100000010C1B2E724F57172A408449F1F109685340);
INSERT INTO Customers (CustomerName, LOC) VALUES ('Customer2',0xE6100000010CBE30992A18152A4093AAED26F8675340);
INSERT INTO Customers (CustomerName, LOC) VALUES ('Customer3',0xE6100000010CBE30992A18152A4093AAED26F8675341);
INSERT INTO Customers (CustomerName, LOC) VALUES ('Customer4',0xE6100000010CBE30992A18152A4093AAED26F8675342);

INSERT INTO Employees (EmployeeName, LOC) VALUES ('Employee1',0xE6100000010C1B2E724F57172A408449F1F109685340);
INSERT INTO Employees (EmployeeName, LOC) VALUES ('Employee2',0xE6100000010CBE30992A18152A4093AAED26F8775340);
INSERT INTO Employees (EmployeeName, LOC) VALUES ('Employee3',0xE6100000010CBE30992A18152A4093AAED26F8885341);
INSERT INTO Employees (EmployeeName, LOC) VALUES ('Employee4',0xE6100000010CBE30992A18152A4093AAED26F8695342);

SELECT TOP(1) CustomerName, EmployeeName, Customers.LOC.STDistance(Employees.Loc) AS DistanceApart
FROM Customers, Employees
WHERE CustomerName = 'Customer2'
ORDER BY CustomerName, DistanceApart;

这将返回给定客户最近的员工。

于 2013-10-04T04:51:30.663 回答
0

我认为返回测量取决于您的地理数据类型的空间参考标识符 (SRID)。默认值为 4326,以米为单位。数据库中有一个表,您可以检查 Select * from sys.spatial_reference_systems

于 2013-10-04T04:18:03.037 回答