在Haversine Formula SQL Query中对记录进行分组和计数

时间:2015-05-31 18:20:23

标签: php mysql sql sql-server haversine

我正在开设车辆交易网站。我有一个名为vehicle的车辆,有20个领域,包括车辆的品牌,型号等。我正在编写一个搜索查询来检索系统组中的所有汽车,并通过make计算它们的计数。

所以我的目标是让系统中的所有车辆按其制造分组,其中包括制造商的计数,但计数部分无法正常工作。它通过忽略我的距离计算标准来返回汽车总数。

我正在执行以下SQL:

SELECT * FROM (SELECT *,ROUND(((ACOS(SIN(51.4811109 * PI() / 180) * SIN(latitude * PI() / 180) + COS(51.4811109 * PI() / 180) * COS(latitude * PI() / 180) * COS((-0.433641 - longitude) * PI() / 180)) * 180 / PI()) * 60 * 1.1515),2) AS distance, count(make) AS carcount FROM `vehicle` `t` WHERE (status='Active')) as v GROUP BY make HAVING distance<=10  

除了carcount之外,它返回的一切都正确,它在系统中返回325辆宝马汽车(系统中的宝马汽车总数),而不是12辆宝马汽车(10英里距离内只有10辆汽车)。 / p>

谁能看到我做错了什么?谢谢你的帮助。

2 个答案:

答案 0 :(得分:2)

你需要在where子句中没有条件

SELECT *, count(*)
FROM 
(SELECT *,ROUND(((ACOS(SIN(51.4811109 * PI() / 180) * 
              SIN(latitude * PI() / 180) + COS(51.4811109 * 
              PI() / 180) *
              COS(latitude * PI() / 180) * 
              COS((-0.433641 - longitude) * PI() / 180)) *
              180 / PI()) * 60 * 1.1515),2) AS distance
FROM `vehicle` `t` 
WHERE (status='Active')) as v
WHERE distance<=10 
GROUP BY make 

答案 1 :(得分:2)

试试这个

#include <iostream>
using namespace std;

void funcA(int i, int j) {
    std::cout << "0: " << i << ", " << j << endl;
}

void funcB(int i, int j) {
    std::cout << "1: " << i << ", " << j << endl;
}

void funcC(int i, int j) {
    std::cout << "2: " << i << ", " << j << endl;
}

int main(int argc, char *argv[]) {
    if (argc < 2) {
        cout << "Usage: ./a.out <val>" << endl;
        exit(0);
    }

    int index = atoi(argv[1]);
    if (index < 0 || index > 2) {
        cout << "Out of bounds" << endl;
        exit(0);
    }

    void(* arr[])(int, int) = { funcA, funcB, funcC };
    arr[index](1, 2);

    return 0;
}