我想为我的客户找到最近的员工。
我有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'
)
答案 0 :(得分: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;
这将返回给定客户的最近员工。
答案 1 :(得分:0)
我认为返回测量取决于您的地理数据类型的空间参考标识符(SRID)。默认值为4326,单位为米。数据库中有一个表,您可以从sys.spatial_reference_systems
中选择Select *