我正在使用SQL Server 2005 Analysis Services,我试图计算一个MDX查询内的距离 - 这样我就可以计算出当前位置附近的物品的数量。我创建了一个纬度为&经度的维度,并且还创建了一个.NET程序集来完成数学运算 - 但我很难在查询中解决这一切。MDX地理距离计算
我的查询找到100英里半径的项目看起来是这样的:
select FILTER([DimProducts].[Product].[Product],
ZipCalculatorLib.GetDistance(43.474208, [Zip].[Latitude], 96.687689, [Zip].[Longitude]) < 100) on rows,
[Measures].[RowCount] on columns
from MyCube;
而在.NET我的距离的代码如下所示:
public static double GetDistance(double startLat, double endLat,
double startLong, double endLong)
{
return Math.Sqrt(Math.Pow(69.1 * (startLat - endLat), 2) + Math.Pow(Math.Cos(endLat/57.3) * 69.1 * (startLong - endLong), 2));
}
然而,当我运行查询,我拿出零记录。如果我将距离从100更改为10000,则可以得到类似于100英里半径的计数。它看起来像.NET类没有做平方根 - 但我已经测试了许多代码,它看起来是正确的。
有没有人有任何建议,我应该看看解决我的问题?
编辑: 我开始怀疑,也许经纬度没有正确传递到我的GetDistance函数 - 所以我在那里添加了一行代码来抛出异常,告诉我他们是什么。我增加了以下内容:
throw new ArgumentException("endLat", string.Format("endLat: {0}, endLong: {1}", endLat, endLong));
,现在当我跑我的查询,我得到以下错误:
Execution of the managed stored procedure GetDistance failed with the following error: Exception has been thrown by the target of an invocation.endLat Parameter name: endLat: 1033, endLong: 1033.
所以,现在的问题就变成了:我怎么得到我的实际纬度值通过在过滤器中的功能?看起来只是一个语言代码正在被传入。
.net代码中的计算是以英里为单位的近似距离。这不是确切的,但足够接近 - 因为我不是指导导弹,只是寻找库存:) SQL 2008不适用于这个项目。 – 2009-10-13 21:25:23