2013-10-04 51 views
0

我想爲我的客戶找到最近的員工。使用STDistance的2個位置之間的距離

我有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

嘗試 選擇emp_id從Emp_Master,Cust_Master 凡Emp_Master.Emp_Location == Cust_Matser.Cust_Location – user1956570

+0

Hi..AM提示以下錯誤:「無效的運算符的數據類型。操作等於相等,類型等於地理。」 – Subash

+0

您的位置是不同的類型? – user1956570

回答

1

檢查SELECT *表或許這將幫助:

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

嗨弗農,感謝您的建議。查詢正在稍作修改。實際上,您建議的查詢是將顧客委託給員工。所以我已經修改它,現在它的工作正常... – Subash

0

我想返回測量取決於你的地理數據類型的空間參考標識符(SRIDs)。默認值是4326,單位爲米。有」在DB你可以從sys.spatial_reference_systems

+0

嗨Goyal..Yes 4326是在米。但實際問題是查詢本身在語法中返回錯誤.. – Subash

相關問題