简体   繁体   English

分组最大多列

[英]Groupwise Max Multiple Columns

Ive written a query that allows me to get the closest rooms using the groupwise max method. 我编写了一个查询,使我可以使用groupwise max方法获取最近的房间。 But how do I do groupwise max for multiple columns? 但是,如何对多列进行分组最大?

I have the query below where the subquery gives me the closest rooms and then joins them with the main table. 我在下面的查询中,子查询为我提供了最近的房间,然后将它们与主表连接起来。 Now I want to pick the first event that will happen next. 现在,我想选择下一个将要发生的事件。

Ex: MIN(DATEDIFF( date, now())) > 0 例如:MIN(DATEDIFF(date,now()))> 0

SELECT name, date, t1.Room, descr, t1.D
FROM Events
JOIN
(
        SELECT Roomid, Room, Latitude, Longitude,
                ACOS(SIN((:lat))*SIN(RADIANS(Latitude)) + COS((:lat))*COS(RADIANS(Latitude))*COS(RADIANS(Longitude)-(:lon)))*(:R) AS D
        FROM Rooms
        WHERE Latitude>(:minLat) AND Latitude<(:maxLat)
                AND Longitude>(:minLon) AND Longitude<(:maxLon)
                AND ACOS(SIN((:lat))*SIN(RADIANS(Latitude)) + COS((:lat))*COS(RADIANS(Latitude))*COS(RADIANS(Longitude)-(:lon)))*(:R) < (:rad)
) AS t1 ON Events.roomid = t1.Roomid
GROUP BY Room
ORDER BY D

Can someone give me any pointers on how to do this? 有人可以给我有关如何执行此操作的指示吗?

Thanks! 谢谢!

PS: The event should be the next occurring event. PS:该事件应该是下一个发生的事件。 I don't use ABS(MIN()) because that will return events that already happened. 我不使用ABS(MIN()),因为这将返回已经发生的事件。

Assuming D is your date, order by date difference and limit 1 假设D是您的日期,按日期差和限制1排序

SELECT name, date, t1.Room, descr, t1.D
FROM Events
JOIN
(
        SELECT Roomid, Room, Latitude, Longitude,
            ACOS(SIN((:lat))*SIN(RADIANS(Latitude)) + COS((:lat))*COS(RADIANS(Latitude))*COS(RADIANS(Longitude)-(:lon)))*(:R) AS D
    FROM Rooms
    WHERE Latitude>(:minLat) AND Latitude<(:maxLat)
            AND Longitude>(:minLon) AND Longitude<(:maxLon)
            AND ACOS(SIN((:lat))*SIN(RADIANS(Latitude)) + COS((:lat))*COS(RADIANS(Latitude))*COS(RADIANS(Longitude)-(:lon)))*(:R) < (:rad)
) AS t1 ON Events.roomid = t1.Roomid
ORDER BY DATEDIFF( D, now())
LIMIT 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM