如何按最近订单的行分组?

时间:2014-05-10 18:22:50

标签: mysql groupwise-maximum

更新:

Here is the demo目前的结果基于M Khalid Junaid的回答。查询仍然没有输出我的预期结果。

我有一个非常简单的表,这里是值。

id      animal_id       latitude    longitude   created_at
--------------------------------------------------------------------------
119     75          1.356203    103.828140  2014-04-30 15:00:04
118     75          1.296613    103.857079  2014-04-30 14:58:58
117     75          1.296613    103.857079  2014-04-30 14:58:20
116     75          1.296613    103.857079  2014-04-30 14:53:17

以下是我的查询,如果纬度经度 user_id 相同,我想要GROUP。

select p.id,p.animal_id,p.name,p.latitude,p.longitude,p.created_at from Photo p
        where 5 >= (select count(*)
                    from Photo p2
                    where p2.animal_id = p.animal_id and
                          p2.id <= p.id
                   )
        AND DATE(p.created_at) > DATE_SUB(NOW(),INTERVAL 13 DAY)
        AND ( p.latitude BETWEEN 0.908862 AND 1.717581 ) AND ( p.longitude BETWEEN 103.584595 AND 104.098206 )
        GROUP BY p.latitude,p.longitude,p.animal_id
        ORDER BY p.created_at DESC;

当前结果 id = 116

id      animal_id       latitude    longitude   created_at
--------------------------------------------------------------------------
119     75          1.356203    103.828140  2014-04-30 15:00:04
116     75          1.296613    103.857079  2014-04-30 14:53:17

预期结果 id = 118

我希望在将纬度,经度,uer_id

分组时获得最新结果
id      animal_id       latitude    longitude   created_at
--------------------------------------------------------------------------
119     75          1.356203    103.828140  2014-04-30 15:00:04
118     75          1.296613    103.857079  2014-04-30 14:58:58

我尝试了几种方法,但无法获得理想的结果。

3 个答案:

答案 0 :(得分:1)

使用自我加入,分组的结果是不确定的,并且无法保证您为该组的最新行

select p.id,p.animal_id,
p.latitude,p.longitude,p.created_at 
from Photo p
JOIN (SELECT MAX(id) id,latitude,longitude,animal_id
 FROM Photo 
GROUP BY latitude,longitude,animal_id
) p1 ON(p.id = p1.id)
where 5 >= (select count(*)
            from Photo p2
            where p2.animal_id = p.animal_id and
            p2.id <= p.id)
AND DATE(p.created_at) > DATE_SUB(NOW(),INTERVAL 13 DAY)       
AND ( p.latitude BETWEEN 0.908862 AND 1.717581 ) 
AND ( p.longitude BETWEEN 103.584595 AND 104.098206 )
GROUP BY p.latitude,p.longitude,p.animal_id
ORDER BY p.created_at DESC;

Demo

答案 1 :(得分:0)

最后我设法找到了自己的答案。谢谢大家分享你的时间。

SELECT p.id,p.animal_id,p.latitude,p.longitude,p.created_at 

-- This will make sure that `GROUP BY` will pickup most recent result
FROM (SELECT id,animal_id,latitude,longitude,created_at from Photo ORDER BY id DESC) p

-- Pick 5 rows for each animal_id. Meaning, there can be multiple photo of animal but MAX is 5.
WHERE 5 >= (select count(*)
                    from (SELECT * from Photo p4 GROUP BY latitude,longitude,animal_id) p2
                    where p2.animal_id = p.animal_id and
                          p2.id >= p.id 
                   )

-- We want Photos uploaded 5 days ago
AND DATE(p.created_at) > DATE_SUB(NOW(),INTERVAL 15 DAY)

-- Plus, only photos within given Map Bound
AND ( p.latitude BETWEEN 0.908862 AND 1.717581 ) AND ( p.longitude BETWEEN 103.584595 AND 104.098206 )                   

-- This will help to remove duplicate images in same location of same animal. We will see only most recent photo in each exact lat,lng location
GROUP BY p.latitude,p.longitude,p.animal_id

ORDER BY id DESC; 

答案 2 :(得分:0)

这可能是棘手的解决方案。您可以在组语句中连接纬度,经度,animal_id ,如下所示:

group by concat(latitude, longitude, animal_id)