自学内容网 自学内容网

【MySQL】MIN()、MAX()比较日期大小

思路:注意题目的两个关键词,每位玩家,所以用GROUP BY

第一次的日期,所以用MIN(DATE)

SELECT  player_id,
        MIN(event_date) AS `first_login`
FROM    Activity AS a
GROUP BY player_id;

这种很方便,不过窗口函数会快点

SELECT  s.player_id,
        s.event_date AS `first_login`
FROM    (
            SELECT  a.player_id,
                    a.event_date,
                    ROW_NUMBER() OVER (
                                    PARTITION BY    a.player_id
                                    ORDER BY a.event_date
                        ) AS n
            FROM    Activity AS a
        ) AS s
WHERE   s.n = 1;

或者这么写

SELECT  DISTINCT a.player_id,
        FIRST_VALUE(a.event_date) OVER (
                    PARTITION BY
                                a.player_id
                    ORDER BY
                                a.event_date
        ) AS first_login
FROM
        Activity AS a;


原文地址:https://blog.csdn.net/lklalmq/article/details/145097080

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!