2014-09-05 174 views
0

表中檢索使用組最晚時間排: -通過不工作

表1: - Person_LatLong

Person_Id 
Lat 
Lng 
Location_DateTime 

Person_LatLong數據: -

10 23.030211421184454 72.55705489668806 2014-09-02 04:23:42 
10 23.03029215373424 72.55710855670746 2014-09-02 04:25:09 
10 23.030301667271765 72.55715279051745 2014-09-02 04:27:21 
11 19.045563510415214 72.915665750359 2014-09-02 08:22:07 
11 19.046567530190785 72.91524330185979 2014-09-02 08:32:36 
11 19.04553070382594 72.91621148133476 2014-09-02 08:39:47 
11 18.537267778519347 73.83538450865574 2014-09-03 01:44:19 
11 18.33554237666039 73.85274219500492 2014-09-03 07:18:02 
11 18.331919816746026 73.8525499279805 2014-09-03 07:18:59 
11 18.33181875247372 73.85243149060277 2014-09-03 07:19:02 
11 18.777939290860722 73.31834934181029 2014-09-04 22:07:31 
11 18.790032969638293 73.30265963437363 2014-09-04 22:09:06 
11 18.79108238318203 73.29928216416553 2014-09-04 22:09:22 
11 18.800857529132163 73.28531940244517 2014-09-04 22:11:22 
11 18.812675453346255 73.27794458217039 2014-09-04 22:13:26 
11 18.82985965773455 73.25592224937081 2014-09-04 22:15:31 
11 18.84531169311457 73.23344887176076 2014-09-04 22:17:49 
11 18.869063931831764 73.2185512231118 2014-09-04 22:19:54 
11 18.893204517796047 73.20479873759578 2014-09-04 22:22:03 
11 18.910161939581506 73.18348844819505 2014-09-04 22:24:30 

表2: - LS_For

Subject_Id -> This is basically Person_Id but I have changed the name. 
Watcher_Id 
Assistance_Group_Id 

LS_For表達TA: -

1 10 1 
1 11 1 
1 17 1 

查詢: -

SELECT *,(6371 * acos(cos(radians(23.030)) 
* cos(radians(Lat)) * cos(radians(Lng) - radians(72.5570)) 
+ sin(radians(23.030)) * sin(radians(Lat)))) 
AS distance FROM Person_LatLong WHERE Person_Id IN 
(SELECT Watcher_Id FROM LS_For WHERE Subject_Id = 1 AND Assistance_Group_Id = 1) 
HAVING distance < 5 ORDER BY Location_DateTime DESC; 

列出了作爲輸出: - ID | LAT | LNG |日期時間|距離

10 23.030301667271765 72.55715279051745 2014-09-02 04:27:21 0.037008818510632306 
10 23.03029215373424 72.55710855670746 2014-09-02 04:25:09 0.03433299317128307 
10 23.030211421184454 72.55705489668806 2014-09-02 04:23:42 0.02417068347133403 

11 23.030301667271765 72.55715279051745 2014-09-02 05:27:21 0.037008818510632306 
11 23.03029215373424 72.55710855670746 2014-09-02 05:25:09 0.03433299317128307 
11 23.030211421184454 72.55705489668806 2014-09-02 05:23:42 0.02417068347133403 

添加GROUP BY後: -

SELECT *,(6371 * acos(cos(radians(23.030)) 
* cos(radians(Lat)) * cos(radians(Lng) - radians(72.5570)) 
+ sin(radians(23.030)) * sin(radians(Lat)))) 
AS distance FROM Person_LatLong WHERE Person_Id IN 
(SELECT Watcher_Id FROM LS_For WHERE Subject_Id = 1 AND Assistance_Group_Id = 1) 
GROUP BY Person_Id HAVING distance < 5 ORDER BY Location_DateTime DESC; 

給了我下面的輸出: -

10 23.030211421184454 72.55705489668806 2014-09-02 04:23:42 0.02417068347133403 

但我想獲得最新的時間排不是最古老的。

需要輸出: - 使用2個表格,LS_For是我在查找Ids的表格,我必須查找位置距離並根據需要根據距離過濾結果。

10 23.030301667271765 72.55715279051745 2014-09-02 04:27:21 0.037008818510632306 
11 23.030301667271765 72.55715279051745 2014-09-02 05:27:21 0.037008818510632306 
+0

能否請你撥弄你模式和樣本數據? – AK47 2014-09-18 12:18:32

回答

0

而不是

GROUP BY Person_I 

使用

LIMIT 1 

OR

GROUP BY Person_Id HAVING distance < 5 ORDER BY Location_DateTime DESC; 

GROUP BY Person_Id HAVING distance < 5 ORDER BY Location_DateTime ASC; 
+0

在這種情況下,它只會給我1個結果,因爲我可能會得到1個以上的Person_Id。在這裏,我不只有一個Person_Id,我的結果中可以有10,11,12。 – Scorpion 2014-09-05 13:28:45

+0

好吧,然後你通過ORDER BY Location_DateTime ASC來做訂單;並做你的小組...編輯我的文章 – 2014-09-05 13:31:03

+0

編輯我的文章... – 2014-09-05 13:32:23

3

生成一組包含最大日期時間(最近的)每個人的數據,然後INNER JOIN它,因此你的基地集islimited僅在person_LatLong最新的條目。

(Select person_ID, max(`Location_DateTime`) mldt FROM person_LatLong group by Person_ID) 

生成最新的經緯度爲每個用戶...所以...然後

可能的1

SELECT a.Person_ID, a.LAT, a.LNG, a.`Location_DateTime`, 
(6371 * acos(cos(radians(23.030)) 
     * cos(radians(a.Lat)) * cos(radians(a.Lng) - radians(72.5570)) 
     + sin(radians(23.030)) * sin(radians(a.Lat)))) AS Distance 
FROM Person_LatLong a 
INNER JOIN (SELECT Person_ID, max(`Location_DateTime`) as mldt 
      FROM Person_latLong 
      GROUP BY Person_ID) P 
    on P.Person_ID = a.Person_Id 
and P.mldt = a.`Location_DateTime` 
LEFT JOIN LS_FOR C 
on a.Person_Id = C.Watcher_ID 
    AND C.Subject_Id = 1 
    AND C.Assistance_Group_Id = 1 
GROUP BY a.Person_ID, a.LAT, a.LNG, a.`Location_DateTime` 
HAVING Distance < 5; 

可能的2

SELECT a.Person_ID, a.LAT, a.LNG, a.`Location_DateTime`, 
(6371 * acos(cos(radians(23.030)) 
     * cos(radians(a.Lat)) * cos(radians(a.Lng) - radians(72.5570)) 
     + sin(radians(23.030)) * sin(radians(a.Lat)))) AS Distance 
FROM Person_LatLong a 
INNER JOIN (SELECT Person_ID, max(`Location_DateTime`) as mldt 
      FROM Person_latLong 
      GROUP BY Person_ID) P 
    on P.Person_ID = a.Person_Id 
and P.mldt = a.`Location_DateTime` 
INNER JOIN LS_FOR C 
on a.Person_Id = C.Watcher_ID 
WHERE C.Subject_Id = 1 
    AND C.Assistance_Group_Id = 1 
GROUP BY a.Person_ID, a.LAT, a.LNG, a.`Location_DateTime` 
HAVING Distance < 5; 

我修改響應治療LS_FOR作爲左連接櫃面有沒有在LS_FOR每個用戶記錄,如果不還是回到人甚至thoug h他們不會有subject_ID或assistance_groupID ....

換句話說,上述應返回用簡單的英語:

最近personLatLong會爲每一個獨特PERSON_ID返還;如果距離爲< 5,並且LS_FOR和PersonLatLong條目中有條目,則只會返回subjectID爲1且asstianceGorupID爲1的記錄。如果LS_FOR中沒有條目,它仍然會返回該人(也許您希望它作爲內連接...)

+0

我不是MYSQL的專家,我根本不知道JOINS。所以我很難改變你的查詢。我現在正在收到錯誤。我在我的問題中也添加了表格結構。 – Scorpion 2014-09-05 13:40:26

+0

添加了我的表格結構,以便您瞭解一些想法。從你的查詢中,我認爲你是匹配的日期時間,但在我的情況下,我不需要匹配日期時間。我只需要從LS_For表中獲取Person_Id並基於這些ID,我需要檢查位置半徑並顯示最佳結果。 – Scorpion 2014-09-05 13:49:52

+0

Person_latLong表具有單個用戶的多個條目。爲了獲得最新的信息,我只是將person_latLong加入到本身的一個子集中,其中只列出了最近的條目。它應該對LS_For限制有0影響。 INNER根據其性質可以記錄,而外部和內部聯接只是關聯記錄。因此,通過加入personId和location_datetime上的子集,我們只能從person_latLong獲得1條記錄(最近的) – xQbert 2014-09-05 14:00:55

0

選中此鏈接。

Retrieving the last record in each group

這裏的解決方案比子查詢更快。

select person1.* from Person_LatLong person1 
    LEFT JOIN Person_LatLong person2 on person1.id = person2.id and 
    person1.dateTiem < person2.dateTime 
    where person2.id IS NULL; 

這會給你最新的記錄。

+0

您是否建議使用左連接作爲接受的答案建議? (閱讀一些有關更大數據集和第二個響應中發生的事情的評論) – xQbert 2014-09-05 13:51:28

+0

我已經使用過它,它工作的很好,比子查詢快得多。 – 2014-09-05 14:10:18

+0

@CharveeShah我已經看到了答案,但正如我的問題中提到的,我不太瞭解DB,特別是連接。我有0知識,這就是我在這裏尋找一些幫助的原因。在你提供的鏈接中,我無法得到所有的東西,並嘗試了很多東西,但都失敗了。之後,只有我在這裏發佈了問題。如果你能通過一些指導來幫助我,那就太好了。 – Scorpion 2014-09-05 17:18:53

0

嘗試這樣做:

SELECT 
    *,(6371 * acos(cos(radians(23.030)) * cos(radians(Lat)) * cos(radians(Lng) - radians(72.5570)) + sin(radians(23.030)) * sin(radians(Lat)))) AS distance 
FROM 
    (select * from Person_LatLong ORDER BY Location_DateTime DESC) as t 
WHERE 
    Person_Id IN (SELECT Watcher_Id FROM LS_For WHERE Subject_Id = 1 AND Assistance_Group_Id = 1) 
GROUP BY 
    Person_Id 
HAVING 
    distance < 5 ;